-
System.IO.File
I m creating a text file with the help of File.Create() method and writing some text in it and then close it
After this i need to open the file to review it by using the follwing sttatement
File.OpenRead("c:\\a.txt");
but this is not working plz can any one help me out
the whole code that m using is as follows:
Code:
FileStream fs = File.Create("c:\\a.txt");
StreamWriter sw = new StreamWriter(fs);
sw.Write("hello;");
sw.Close();
fs.Close();
if (MessageBox.Show("Data has been exported successfully. Do you want to review the file?", "question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
File.OpenRead("c:\\a.txt");
-
Re: System.IO.File
Always read the help documentation, especially when things don't work as you expect. 'Create' returned a FileStream, on which you had to create a StreamWriter to write the data to the file. Now that you're calling OpenRead, what do you suppose you have to do to read the data. The help topic for OpenRead even has a code example.
I should point out the CreateText and OpenText methods are better choices for text files because they return StreamWriter and StreamReader objects directly. If you're using .NET 2.0 (please specify your version EVERY time you start a thread) then you might also look at the WriteAllText and ReadAllText methods, which don't require you to explicitly create any I/O objects at all.
-
Re: System.IO.File
thnx alot u r very right ... yea m using .NET 2.0 and i'm going for ur suggestions
just one thing more if u would help ... if i need to open any file like excel or text file from my code then what should i do?
by opening i mean to open separate note pad application sart and open a file in it . just like when we double click a text file it gets open ...
-
Re: System.IO.File
To open a data file in it's default application you pass the file path to Process.Start. If the file type has no default application then an exception will be thrown, so you'd have to catch that if there's a chance of that happening.