简体   繁体   English

打开文件对话框,将文件路径放在var中

[英]Opening a file dialog , taking the file path in a var

I am able to open a file dialog, now i want to know how do i get the path of the file in var variable something like 我能够打开文件对话框,现在我想知道如何在var变量中获取文件的路径,例如

        OpenFileDialog fd1 = new OpenFileDialog();
        fd1.InitialDirectory = "c:\\";
        fd1.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
        fd1.FilterIndex = 2;
        fd1.RestoreDirectory = true;

so i want in my var something like 所以我想要在我的var里像

       var path = @"c:\abc.pdf";

Is it possible 可能吗

Here it is: 这里是:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var path = openFileDialog1.FileName;
            }

This way you'll get your path to file like: 这样,您将获得如下文件路径:

C:\\folder1\\folder2\\fffffffff...\\abc.pdf C:\\ folder1 \\ folder2 \\ fffffffff ... \\ abc.pdf

Update: 更新:

you'll change your "var" into "string" and you'll make your path variable a global variable. 您将“ var”更改为“ string”,并将路径变量设为全局变量。 here is an example: 这是一个例子:

private string path;

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path = openFileDialog1.FileName;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(path);
        }

you don't need to make your variable a public because you are in the same class!!! 您不需要公开变量,因为您属于同一班!!!

Update: 更新:

Think that this will do 认为这会做

AxAcroPDF1.src = path;

The Process.Start should launch a new process to open the pdf file with default client that is Adobe Reader. Process.Start应该启动一个新进程,以使用默认客户端Adobe Reader打开pdf文件。

You can prompt user with filedialog to get you a file path. 您可以使用filedialog提示用户获取文件路径。 If you want to get some of specific folders you can try 如果要获取某些特定文件夹,可以尝试

 String PersonalFolder = 
    Environment.GetFolderPath(Environment.SpecialFolder.Personal);

Environment have alot of folders that are specific for machine. 环境中有很多机器专用的文件夹。 hope it helps 希望能帮助到你

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

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