Skip to content

Saving a string to a file

Rasmus Flotiin

Asked Mar 6, 2007, 11:48 AM EST2 answers
Share
LinkedInX
Hey I hope somebody can help. I am trying to save a string to a text file. I am able to creat a file but I dont know how to put the context of the string down to the file. Hope somebody can help. Creating the save file. SaveFileDialog saveCdb = new SaveFileDialog(); saveCdb.Title = "Save Database configuration"; saveCdb.InitialDirectory = @"c:\"; saveCdb.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; saveCdb.FilterIndex = 2; saveCdb.RestoreDirectory = true; if (saveCdb.ShowDialog() == DialogResult.OK) { saveCdb.FileName= dbtxt.Text; } With kind regards R.F

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    Not sure what your code is doing there, but here is the jist.

    Use the SaveFileDialog to get the filename.

    Use the StreamWriter class to create the file and output your context.

    string myContext = "To code or not to code that is the...";

    StreamWriter sw = new StreamWriter(saveCdb.FileName);

    sw.Write(myContext);

    sw.Close();  // don't forget to close the stream

    -Mike G.
    0
  • 1 short reply
    • 0

      Thank you got it work now

      0