简体   繁体   English

如何为用户提供将文件保存在 C# 中所需位置的选项?

[英]How to give user an options to save file in there desired location in C#?

I know how to create and save a file in C# console application but what if i want the user to choose the location of where they want to save it?我知道如何在 C# 控制台应用程序中创建和保存文件,但是如果我希望用户选择他们想要保存的位置怎么办? i have no idea on how i can make this possible.我不知道如何使这成为可能。

edit- ive realised that would be very hard to input the location the user wants to save the file to however is it possible to save the file as you would do when creating a windows word document, so the user would be able to see where they want to save the file Example编辑意识到输入用户想要保存文件的位置是非常困难的,但是是否可以像创建 windows word 文档时那样保存文件,这样用户就可以看到他们的位置想要保存文件示例

If you want a full console application, that is no windows being created, then there's only one proper thing to do: require the user to specify the save location on the command line.*如果您想要一个完整的控制台应用程序,即不创建窗口,那么只有一件正确的事情要做:要求用户在命令行上指定保存位置。*

Given the fact that you have a console application, you probably already do some checking of the command line, but if not, then the command line can be read from the args argument to your Program.Main :鉴于您有一个控制台应用程序,您可能已经对命令行进行了一些检查,但如果没有,则可以从Program.Mainargs参数读取命令行:

static void Main(string[] args)
{
    ...
}

There are examples on the internet to handle the command line, if you get stuck, or a new question can be asked specifically for the issue you are having.互联网上有处理命令行的示例,如果您卡住了,或者可以专门针对您遇到的问题提出新问题。


*) Now, for the reason why this is the only proper way: *) 现在,这是唯一正确方法的原因:

If the user needs to pass it on the command line, then the user has all the usual niceties such as tab completion available.如果用户需要在命令行上传递它,那么用户可以使用所有常用的细节,例如选项卡完成。 The user can also use dir and cd before calling your program to find the proper directory.用户还可以在调用程序之前使用dircd来查找正确的目录。

On the other hand, if you ask the user to input it, then the user will not have tab completion, will not be able to use dir or cd , and as such will have to type it out manually all the way.另一方面,如果您要求用户输入它,那么用户将无法完成制表符,将无法使用dircd ,因此必须一直手动输入。 Typo's or mistakes are almost guaranteed.错别字或错误几乎是肯定的。

From a user experience point of view, this is very annoying.从用户体验的角度来看,这很烦人。 Programmers should therefore not ask the user to manually type out file paths during program execution.因此程序员应该询问用户程序执行过程中手动键入出的文件的路径。 Hence, it must be specified on the command line.因此,它必须在命令行中指定。

Read about SaveFileDialog阅读SaveFileDialog

private void button1_Click(object sender, System.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)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
}

And ready to test example from msdn example并准备测试来自 msdn 示例的示例

if you want to play with something like >cd (ChDir) then look at如果你想玩像 >cd (ChDir) 之类的东西,那么看看

Environment.CurrentDirectory

or/and force user to write good directory by himselft and use @Carl aswere :) but remember we leave in 21 century people are lazy或/并强迫用户自己编写好的目录并使用@Carl :) 但请记住我们离开 21 世纪人们很懒惰

It is not hard.这并不难。 You need a console to read user's input with string path = Console.ReadLine();您需要一个控制台来读取用户的输入string path = Console.ReadLine(); . . User will input prefered path and it will store into path variable.用户将输入首选路径并将其存储到path变量中。 Now you should check if this path exits if(Directory.Exists(path)) it will return true if path exists false if path does not exist.现在您应该检查此路径是否存在if(Directory.Exists(path))如果路径存在则返回 true 如果路径不存在则返回 false。

Code example:代码示例:

Console.WriteLine("Insert a path: ");
string path = Console.ReadLine();

if(Directory.Exists(path)){
//save logic
}
else{
//path does not exist handler
}

Note: if you want to access Directory class you should use System.IO namespace.注意:如果你想访问 Directory 类,你应该使用System.IO命名空间。

You can use the SaveFileDialog as Taumantis said but you have to add the您可以使用 Taumantis 所说的 SaveFileDialog 但您必须添加

System.Windows.Froms系统.Windows.Froms

namespace and you must mark your main method as a single apartment thread命名空间,您必须将主方法标记为单个单元线程

 class Program
{
    [STAThread]
    static void Main(string[] args)
    {
  SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {

        }


        Console.ReadKey();
    }

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

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