wpf 在 C# 中创建和保存文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21562082/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:43:46  来源:igfitidea点击:

Creating and saving files in C#

c#wpffile-iosavefiledialog

提问by Eric after dark

I need to create and write to a .dat file. I'm guessing that this is pretty much the same process as writing to a .txt file, but just using a different extension.

我需要创建并写入 .dat 文件。我猜这与写入 .txt 文件的过程几乎相同,但只是使用不同的扩展名

In plain english I would like to know how to:

用简单的英语,我想知道如何:

-Create a .dat file

- 创建一个 .dat 文件

-Write to it

- 写信给它

-And save the file using SaveFileDialog

- 并使用保存文件 SaveFileDialog

There are a few pages that I've been looking at, but I think that my best explanation will come from this site because it allows me to state exactly what I need to learn.

我一直在查看一些页面,但我认为我最好的解释来自这个站点,因为它允许我准确地说明我需要学习的内容。

The following code is what I have at the moment. Basically it opens a SaveFileDialogwindow with a blank File:section. Mapping to a folder and pressing save does not save anything because there is no file being used. Please help me use this to save files to different locations.

以下代码是我目前所拥有的。基本上它会打开一个SaveFileDialog带有空白File:部分的窗口。映射到文件夹并按保存不会保存任何内容,因为没有使用文件。请帮助我使用它来将文件保存到不同的位置。

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "";
dlg.DefaultExt = "";

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
    string filename = dlg.FileName;
}

Pages that I've been looking at:

我一直在看的页面:

-http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

- http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

-http://social.msdn.microsoft.com/Forums/en-US/cd0b129f-adf1-4c4f-9096-f0662772c821/how-to-use-savefiledialog-for-save-text-file

- http://social.msdn.microsoft.com/Forums/en-US/cd0b129f-adf1-4c4f-9096-f0662772c821/how-to-use-savefiledialog-for-save-text-file

-http://msdn.microsoft.com/en-us/library/system.io.file.createtext(v=vs.110).aspx

- http://msdn.microsoft.com/en-us/library/system.io.file.createtext(v=vs.110).aspx

回答by Olivier Jacot-Descombes

Note that the SaveFileDialogonly yields a filename but does not actually save anything.

请注意,SaveFileDialog仅生成文件名但实际上并未保存任何内容。

var sfd = new SaveFileDialog {
    Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*",
    // Set other options depending on your needs ...
};
if (sfd.ShowDialog() == true) { // Returns a bool?, therefore the == to convert it into bool.
    string filename = sfd.FileName;
    // Save the file ...
}

Use the filename you are getting from the SaveFileDialogand do the following:

使用您从 中获得的文件名SaveFileDialog并执行以下操作:

File.WriteAllText(filename, contents);

That's all if you intend to write text to the file.

如果您打算将文本写入文件,这就是全部。

You can also use:

您还可以使用:

File.WriteAllLines(filename, contentsAsStringArray);

回答by Rick S

using(StreamWriter writer = new StreamWriter(filename , true))
{
  writer.WriteLine("whatever your text is");
}