-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiling_streamwriter_streamreader.cs
49 lines (39 loc) · 1.23 KB
/
filing_streamwriter_streamreader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
//creating directory
DirectoryInfo di = new DirectoryInfo("D:\\MyDirectory");
di.Create();
//creating file and file stream
FileInfo fi = new FileInfo("D:\\MyDirectory\\vp.txt");
FileStream fs = fi.Create();
//checking if the file is created or not
if (File.Exists("D:\\MyDirectory\\vp.txt"))
Console.WriteLine("File created");
else
Console.WriteLine("File not created");
//creating a stream writer object
StreamWriter sw = new StreamWriter(fs);
//writing to file
sw.WriteLine("Adil Aslam Sachwani");
sw.WriteLine("Hello World");
sw.Close();
fs.Close();
try{
//creating a stream reader object
StreamReader sr = new StreamReader("D:\\MyDirectory\\vp.txt");
//reading from file
string str;
while ((str = sr.ReadLine()) != null)
Console.WriteLine(str);
sr.Close();
}
catch(FileNotFoundException e){
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}