简体   繁体   中英

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

        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 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

Update:

you'll change your "var" into "string" and you'll make your path variable a global variable. 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.

You can prompt user with filedialog to get you a file path. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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