0

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

3
  • 1
    Is it actually under the same directory of your executable program? If it is in any random directories, there is no way other than prompting the user to select.
    – Alvin Wong
    Commented Nov 29, 2012 at 7:09
  • I still don't understand (i.e. you didn't answer my comment) so what do you expect me to tell you?
    – Alvin Wong
    Commented Nov 29, 2012 at 7:30
  • you can add pdf as project file using Add Existing Item dialog and change Build Action to Copy If Newer, it such way it will be copied to applications directory on build, so you'll have to find out application's start up path and you'll know where your pdf is.
    – Giedrius
    Commented Nov 29, 2012 at 7:32

2 Answers 2

1

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.

3
  • since var is not declared public and is methodspecific and i have two private functions in my name space how do i make use of var path in another private function inside the same class in the same app Commented Nov 29, 2012 at 8:40
  • but then since i am making use of axAcroPDF1.LoadFile(path); the file does not load on this container Commented Nov 29, 2012 at 8:55
  • is there any possiblity to make replicate the use of var here Commented Nov 29, 2012 at 9:01
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.