compugeek Posted September 20, 2003 Posted September 20, 2003 I know that with VB you used the Open statement, and the Append type. So, how would I do this same thing in VB.NET? To open a file, create it if it does not exist, and allow it to be written to? Then what's the type for reading the file? Thanks. Quote
*Experts* mutant Posted September 20, 2003 *Experts* Posted September 20, 2003 Use the IO.StreamWriter object to write to the file: Dim writer As New IO.StreamWriter("path to the file", True) First argument is the path to the file, and the second argument which is a boolean indicates whether you want to append to the file. To open a file and read it use the IO.StreamReader object: Dim reader As New IO.StreamReader("path to the file") 'then read it using different methods... reader.ReadLine() 'or reader.ReadToEnd() 'or other reading methods you want to use that you want to use Quote
compugeek Posted September 21, 2003 Author Posted September 21, 2003 (edited) Hey, cool, I'll try that - thanks! If it doesn't work, I'll let ya know, but I'm sure it will ;) EDIT - How do you set the program's current location? In the old VB, it was something like App.Path, but that's probably not the same in .NET. And how do you actually write to a file? I've tried writer.WriteLine("Hello") or using a variable: writer.WriteLine(curFunds), but it still is not working... ? Edited September 21, 2003 by compugeek Quote
aewarnick Posted September 21, 2003 Posted September 21, 2003 There is plenty of docs on the net about this stuff. Do you mean Application.ExecutablePath /// <summary> /// Writes a text file to disk. /// </summary> public static void WriteText(string path, object data, bool Append) { StreamWriter Sw= new StreamWriter(path, Append); try { Sw.Write(data); } catch{MessageBox.Show("Could not write file.", "Error");} Sw.Close(); } //---------- public static void WriteText(string path, object o) { StreamWriter Sw= new StreamWriter(path); try { Sw.Write(o); } catch{MessageBox.Show("Could not write file.", "Error");} Sw.Close(); } Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.