简体   繁体   English

如何将文件保存在用户选择的路径中

[英]How to save the file in a path chosen by the user

I am importing a source file and processing it and then after that I have to save it in a new location. 我要导入源文件并进行处理,然后再将其保存在新位置。 I have created the syntax for importing file, tell me the syntax to save it to a new location. 我已经创建了导入文件的语法,请告诉我将其保存到新位置的语法。 One is when I am calling the constructor to give the path of import file, then I can also give the path for the output location. 一种是当我调用构造函数提供导入文件的路径时,然后我也可以提供输出位置的路径。 But don't know how to implement it. 但是不知道如何实现它。 Please tell. 请告诉。

You can use a SaveFileDialog pretty much like this: 您可以像这样使用SaveFileDialog:


using ( var dlg = new SaveFileDialog() )
{
    if ( dlg.ShowDialog() == DialogResult.OK )
    {
        //SAVE THE OUTPUT
        //DEPENDING ON THE FORMAT, YOU MAY WANT TO USE
        //File.WriteAllBytes(dlg.FileName, yourBytes);
        //File.WriteAllText(dlg.FileName, yourText);
        //File.WriteAllLines(dlg.FileName, yourStringArr);
        //OR ANY OTHER CODE YOU WANT TO USE TO PERSIST YOUR DATA
    }
        //else the user clicked Cancel
}

Also, you can set a default extension, a default path and more. 另外,您可以设置默认扩展名,默认路径等。 Look up SaveFileDialog's information on MSDN 在MSDN上查找SaveFileDialog的信息

导入文件时,可以使用StreamWriter基类将其写入最终用户在文本框或文件上传框中指定的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM