如何以txt格式保存文件 [英] How to save files in txt format
本文介绍了如何以txt格式保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我想将txtWritingBox的内容保存到新文件中。
private void mnuSaveFile_Click( object sender,EventArgs e)
{
流myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = TXT文件(*。 txt)| * .txt | CSV文件(* .csv)| * .csv;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
if (saveFileDialog1.ShowDialog()== DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile())!= null )
{
// 写入流的代码就在这里。
myStream.Close();
}
}
}
如何保存来自文本框内容的文件
解决方案
试试这个
File.AppendAllText(Path,Content);
这将解决您的2个问题。
1)如果没有创建,它将创建文件。
2 )如果文件存在,它将用新内容覆盖它
参见 http://msdn.microsoft.com/en-us/library/system.io.stream(v = vs.110).aspx [ ^ ]
In my code I want to save the contents of "txtWritingBox" to a new file.
private void mnuSaveFile_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "TXT Files (*.txt)|*.txt|CSV files (*.csv)|*.csv";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
}
How to save the file from textbox content
解决方案
try this
File.AppendAllText("Path","Content");
this will resolve your 2 problems.
1) it will create the file if not created.
2) if the file exists it will overwrite it with new content
See http://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx[^].
这篇关于如何以txt格式保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文