Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# saveDialog box

Options
  • 23-03-2007 12:01am
    #1
    Registered Users Posts: 1,987 ✭✭✭


    I have the below code, it creates the file everytime i click save but i can't figure out how to pass the text from a textfield into the file when its saving!??
    private void cmdSave_Click(object sender, System.EventArgs e)
    {
    	SaveFileDialog sf = new SaveFileDialog();
    	sf.Filter = "Text Files (*.txt)|*.txt|" + "All Files|";
    	sf.ShowDialog();
    }
    


Comments

  • Registered Users Posts: 541 ✭✭✭Vorrtexx


    The SaveFileDialog is used to specify a filename on where to save a file to.
    It's up to yourself to take that location specified and create the file there.

    ShowDialog() will return a DialogResult.OK when the Save button is clicked on the SaveFileDialog form. After this you need to write code to save the value in your textbox to that location.

    Something as follows for example:
    SaveFileDialog sf = new SaveFileDialog();
    sf.Filter = "Text Files (*.txt)|*.txt|" + "All Files|";
    if(sf.ShowDialog() == DialogResult.OK)
    {
    	System.IO.StreamWriter myFile = new System.IO.StreamWriter(sf.FileName);
    	myFile.Write(textBox1.Text);
    	myFile.Close();
    }
    

    Make sure to check for errors also and wrap the code in a try/catch in case of Permission errors or any other File IO exceptions that could occur.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Nice one, got it sorted!


Advertisement