Reading A File...

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,



Answer this question

Reading A File...

  • Robert Beaubien

    Figured it out. Thanks for help, guys.
  • Aaron Hallberg - MSFT

    In Java, I used something called a 'StringTokenizer'.  Is there a substitute for this within C#   The file that i am reading contains the following: "one", "2", "three".  I need to get all three values out of line.  I am using ReadLine to take the line out of the file, but I need to seperate all 3 values.
  • Mark Ainsworth

    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.



  • Brian Layden

    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,


  • MBowen

    I think you are looking for String.Split.

  • Andrew O.

     

    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.



  • Reading A File...