使用 C# 编写文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14449407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a TEXT file using C#
提问by Jeagr
I am trying to add a SaveFileDialog to my Windows Application, in which I save a simple text file from the contents of a multi-line text box. The program seems to work fine up until the save dialog, and I don't get any errors, but once I click the save button the application just hangs. The only way out of it is to click the "stop-debugging" button. Here are the code sections that I have tried, but both create the same hang:
我正在尝试将 SaveFileDialog 添加到我的 Windows 应用程序,在其中我从多行文本框的内容中保存了一个简单的文本文件。该程序似乎在保存对话框之前运行良好,并且我没有收到任何错误,但是一旦我单击保存按钮,应用程序就会挂起。唯一的方法是单击“停止调试”按钮。以下是我尝试过的代码部分,但都创建了相同的挂起:
private void button_SaveToFile_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, textBox_ListDestination.Text);
}
}
and
和
private void button_SaveToFile_Click(object sender, EventArgs e)
{
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)
{
File.WriteAllText(saveFileDialog1.FileName, textBox_ListDestination.Text);
myStream.Close();
}
}
}
采纳答案by Mark Headley
Jeagr,
杰格,
I don't have any issues when I try to recreate the problem using your first example code. If I were to guess, you may be having problems with memory allocation (working with unmanaged resources) coupled with multiple attempts at debugging, and maybe re-saving to the same file over and over may have left a bad file pointer.
当我尝试使用您的第一个示例代码重新创建问题时,我没有任何问题。如果我猜测,您可能会遇到内存分配问题(使用非托管资源)以及多次尝试调试,并且可能一遍又一遍地重新保存到同一个文件可能会留下错误的文件指针。
You second example will not work. When you call File.WriteAllText, it opens, writes and closes the file for you automatically. (Read here: http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx)
你的第二个例子不起作用。当您调用 File.WriteAllText 时,它会自动为您打开、写入和关闭文件。(在这里阅读:http: //msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx)
When you call OpenFile, you are placing a lock on that file. In your code, when File.WriteAllText is executed, it blows up because the file is already in use. If you want to work with your file using OpenFile, then you will have to change how you work with the file. Here is an example: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.openfile(v=VS.71).aspxNote how the example closes the file, if you wanted to do working in the file, you would code that work before the CloseFile method is called.
当您调用 OpenFile 时,您将锁定该文件。在您的代码中,当 File.WriteAllText 执行时,它会爆炸,因为该文件已在使用中。如果要使用 OpenFile 处理文件,则必须更改处理文件的方式。这是一个示例:http: //msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.openfile(v=VS.71).aspx请注意该示例如何关闭文件(如果需要)要在文件中工作,您需要在调用 CloseFile 方法之前编写工作代码。
If you would like an example on how to work with a file using OpenFile, there should be a couple of examples on the internet, but based on your need, I think File.WriteAllText will work.
如果你想要一个关于如何使用 OpenFile 处理文件的例子,互联网上应该有几个例子,但根据你的需要,我认为 File.WriteAllText 会起作用。
Back to the first example.
回到第一个例子。
If you are hitting the code multiple times during debugging, my only suggestion would be to wrap the SaveFileDialog in a 'using' statement. This may help with the system hanging, and some debugging.
如果您在调试期间多次点击代码,我唯一的建议是将 SaveFileDialog 包装在“使用”语句中。这可能有助于系统挂起和一些调试。
private void button1_Click(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog())
{
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
sfd.FilterIndex = 2;
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox_ListDestination.Text);
}
}
}
回答by Kitet
Although i don't use C# but C++, i HAD this problem (namely dialog not appearing at all). Solved it by zeroing whole OPENFILENAME structure, then filling members i only need. In fact, not zeroing structures prior to invoking common dialogs was also a problem with print dialog.
虽然我不使用 C# 而是 C++,但我遇到了这个问题(即根本没有出现对话框)。通过将整个 OPENFILENAME 结构归零来解决它,然后填充我只需要的成员。事实上,在调用公共对话框之前不将结构归零也是打印对话框的一个问题。
回答by Jeagr
I ended up using the code below to get this to work. The code "ShowHelp = true" is what solved the problem, and then a little optimizing led to the following solution:
我最终使用下面的代码来让它工作。代码“ShowHelp = true”就是解决问题的,然后稍微优化导致了以下解决方案:
private static void SaveToFile(string List)
{
var saveFileDialog1 = new SaveFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal),
Filter = string.Format("{0}Text files (*.txt)|*.txt|All files (*.*)|*.*", "ARG0"),
RestoreDirectory = true,
ShowHelp = true,
CheckFileExists = false
};
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
File.WriteAllText(saveFileDialog1.FileName, keywordList);
}