I have created a form with a menu, which contains the menuitem 'Open'. What I want to happen is that when the user selects the 'Open' menuitem, the user is able to specify a file to open.
Note: The file just contains the following text: "1,2,3".
I will then want to read the text from the file and modify it.
I'm not too sure how to go about doing this. Any help is appreciated.
Thanks,

Reading A File...
Tom25
Hi,
Just do the following steps…
Put a text box on the form (optional:set its multi line property to true and dock fill).
Write following code in Open menu
OpenFileDialog opd = new OpenFileDialog();
if (DialogResult.OK==(opd.ShowDialog()))
textBox1.Text = ReadFile(opd.FileName);
Write following code in Save menu
SaveFileDialog sfd = new SaveFileDialog();
if (DialogResult.OK==(sfd.ShowDialog()))
SaveFile(sfd.FileName);
Write following functions on the form to open and read file and return a string
string ReadFile(string path)
{
try
{
StreamReader sr = new StreamReader(path);
string data=sr.ReadToEnd();
sr.Close();
return data;
}
catch (Exception exception)
{
//handle error
return "";
}
Write following function on the form to read text from text box and save to file
void SaveFile(string path)
{
try
{
StreamWriter sw = new StreamWriter(path);
sw.Write(textBox1.Text);
sw.Flush();
}
catch (Exception ex)
{
//error handling
}
}
Hope this help.
Remi 42
Mike Gustafson
Alexey Nayda
Create an instance of the Open File Dialog and fill the properties. Then show the Dialog.
OpenFileDialog openFiles = new OpenFileDialog ( ); openFiles.InitialDirectory = "C:\Temp\";openFiles.Filter = "Text files (*.txt)|*.txt";
openSsnKount.FilterIndex = 1;
openSsnKount.RestoreDirectory = false; if ( openSsnKount.ShowDialog ( ) == DialogResult.Cancel )
{
openSsnKount.Dispose ( );
}
That's about it except what you want to do with the selected file.
k3nai
Kent Ogden
The String.Split is working great. However, lets say I have the following string: "one", "two", "three". How could I store each of these values into a seperate variable
I currently have the following code:
string
line = sr.ReadLine(); char[] seps = new char[]{','}; foreach (string ss in line.Split(seps)){
how would i make it so:
string 1 = one
string 2 = two
string 3 = three
}
Thanks Again,