# Visual C# - Save File



## Damion

Hi I have just about finished a gradebook program the only thing i need is to be able to save files. the only thing i can fined is the save file dialog ( code below) but that dosent accualy save the file. can anyone help me?



Code:


// code for save dialog box

private void btnSaveFile_Click(object sender, System.EventArgs e)
		{
			saveFileDialog1.Title = "Specify Destination Filename";
			saveFileDialog1.Filter = "Text Files|*.txt";
			saveFileDialog1.FilterIndex = 1;
			saveFileDialog1.OverwritePrompt = true;

			if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
				txtDestination.Text = saveFileDialog1.FileName;
		}


----------



## Damion

ok i looked around a bit more and found the code ( listed below) but it dosent tell me were to put it. does any one know?


Note: 
- It does not go in the save file dialog.
- It does not go in the button that runs the save file dialog.



Code:


// create a file stream, where "c:\\testing.txt" is the file path
			System.IO.FileStream fs = new System.IO.FileStream("c:\\testing.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);

			// create a stream writer
			System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.ASCII);

			// write to file (buffer), where textbox1 is your text box
			sw.Write(txtDestination.Text);

			// flush buffer (so the text really goes into the file)
			sw.Flush();

			// close stream writer and file
			sw.Close();fs.Close();


----------



## Deckard

Your first code box gets the filename from the user, your second code box actually saves the file. However, you'll need to replace "c:\\testing.txt" with saveFileDialog1.FileName. I assume txtDestination.Text has the contents of whatever you're trying to save.


----------



## Damion

Thanks! ill try this out tommorow i have to go to work now. :grin:


----------

