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);
}
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);
}