using SaveFileDialog to write a text file--not as easy as it looks


R

RayLopez99

This first example below, which I got from MSDN, worked fine, but the
second example did not, just FYI.

as modified from: http://msdn.microsoft.com/en-us/library/sfezx97z(VS.85).aspx

//this worked

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
// Displays a SaveFileDialog
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "RTF file|*.rtf|Text file|*.txt";
saveFileDialog1.Title = "Save an RTF or Text File";
saveFileDialog1.ShowDialog();

// If the file name is not an empty string open it for
saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the
OpenFile method.
System.IO.FileStream fs = (System.IO.FileStream)
saveFileDialog1.OpenFile();
// Saves the file in the appropriate format based upon
the file type selected in the dialog box.

using (TextWriter writter = new StreamWriter(fs))
{
//your code here…ascii text saved... for example // writter.WriteLine
("hi world");
}

fs.Close();
} }

/////////////

But this example did not quite work (possibly because it was using the
'generic' base class Stream):
http://www.dotnetspider.com/resources/4920-How-use-SaveFileDialog-save-text-files.aspx

Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|
*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
StreamWriter wText =new StreamWriter
(myStream);

wText.Write(" your text");

myStream.Close();
}
}


//

BTW, I had no problem using SaveFileDialog to save binary files (not
shown), and I can use a variant of the above where I do not use
SaveFileDialog with no problem, namely this version below where you
are "hard coding" the path and path name (without using
SaveFileDialog), but this example above is for SaveFileDialog.

RL

string pathOne = System.IO.Path.GetDirectoryName
(System.Reflection.Assembly.GetExecutingAssembly().Location);
string pathTwo = "\\myDebugTextFile";

//note you are "hard coding" the name of the file and the path as
shown by the above strings

string path12 = pathOne + pathTwo;

try
{
using (FileStream fs = new FileStream(path12,
FileMode.Append)) //append to file
{
using (StreamWriter myWrighter = new
StreamWriter(fs, System.Text.Encoding.ASCII))
{
myWrighter.WriteLine("Hi World—debug
version”);

}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
 
Ad

Advertisements

M

monteithm

This first example below, which I got from MSDN, worked fine, but the
second example did not, just FYI.

/////////////

But this example did not quite work (possibly because it was using the
'generic' base class Stream):http://www.dotnetspider.com/resources/4920-How-use-SaveFileDialog-sav...

Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|
*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
            if((myStream = saveFileDialog1.OpenFile()) !=null)
            {
                        StreamWriter wText =newStreamWriter
(myStream);

                        wText.Write(" your text");

                        myStream.Close();
            }

}

//

One solution is to add wText.Flush(); before closing myStream.

Mike
 
Ad

Advertisements

R

RayLopez99

One solution is to add wText.Flush(); before closing myStream.

Mike

Thanks Mike. That worked fine. Before I was getting zero sized files
(no writing) but adding that one line wText.Flush(); just before
myStream.Close(); did the trick, and this also works as good as the
first example I gave in the OP.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top