Skip to content

Instantly share code, notes, and snippets.

@davidkevork
Created April 25, 2016 11:50
Show Gist options
  • Save davidkevork/d13d469ff85b7b13bb92ddb1f014a9bc to your computer and use it in GitHub Desktop.
Save davidkevork/d13d469ff85b7b13bb92ddb1f014a9bc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Do you want to create file or open an existing one? create(c)/open(o): ");
string d = Console.ReadLine();
switch (d)
{
case "c":
Console.Write("Enter the name of the file you want to create: ");
string name = Console.ReadLine();
while (name.Length == 0)
{
Console.Write("File name canno't be empty, Please enter a file name: ");
name = Console.ReadLine();
}
FileStream F = new FileStream(name, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
Console.Write("Do you want to write anything in the file? y/n: ");
string yesorno = Console.ReadLine();
if (yesorno == "y")
{
Console.WriteLine("Type in the text you want to write in the file");
string WhatToWrite = Console.ReadLine();
StreamWriter writer = new StreamWriter(F);
writer.WriteLine(WhatToWrite.ToString());
Console.WriteLine("Fille written successfully");
Console.WriteLine("Press any key to exit...");
writer.Close();
}
else
{
Console.WriteLine("Press any key to exit...");
}
F.Close();
break;
case "o":
Console.Write("Enter the name of the file you want to open: ");
string namea = Console.ReadLine();
while (namea.Length == 0)
{
Console.Write("File name canno't be empty, Please enter a file name: ");
namea = Console.ReadLine();
}
Console.Write("Do you want to read the file or write? Read(r)/write(w)/none(n): ");
string yesornoexit = Console.ReadLine();
if (yesornoexit == "r")
{
FileStream Fr = new FileStream(namea, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(Fr);
Console.WriteLine("------------------");
Console.WriteLine(sr.ReadToEnd());
Console.WriteLine("------------------");
Console.WriteLine("File readed successfully");
Console.WriteLine("Press any key to exit...");
sr.Close();
Fr.Close();
}
else if (yesornoexit == "w")
{
FileStream Fw = new FileStream(namea, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(Fw);
Console.WriteLine("Type in the text you want to write in the file");
string WhatToWritew = Console.ReadLine();
sw.WriteLine(WhatToWritew.ToString());
Console.WriteLine("File written successfully");
Console.WriteLine("Press any key to exit...");
sw.Close();
Fw.Close();
}
else
{
Console.WriteLine("Press any key to exit...");
}
break;
default:
Console.WriteLine("Sorry that option does not exist");
Console.WriteLine("Press any key to exit...");
break;
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment