user1862352
user1862352

Reputation: 1

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

Upvotes: 0

Views: 225

Answers (2)

Sylca
Sylca

Reputation: 2545

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.

Upvotes: 1

Gustav Klimt
Gustav Klimt

Reputation: 440

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

Upvotes: 0

Related Questions