Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
  • *Experts*
Posted

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

Posted (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 by compugeek
Posted

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();
		}

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...