1

I am trying to open a file which a user can set. In other words it will never be a set path or file. so when the user has selected the file they want to open this button below will open it. I have declared l1 and p1 as public strings.

    public void button4_Click(object sender, EventArgs e)
    {

         DialogResult result = openFileDialog1.ShowDialog();
         if (result == DialogResult.OK)
         {

             l1 = Path.GetFileName(openFileDialog1.FileName);
             p1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);

         }


    public void button2_Click(object sender, EventArgs e)
    {
    //p1 = directory path for example "C:\\documents and settings\documents"
    //l1 = filename

        Process.Start(p1+l1);
    }

So just to review I want to open up the file just using the directory path and filename. Is this possible? I can just have p1 there and it will open an explorer showing me that directory. Thank you for looking.

Marshal
  • 1,177
  • 4
  • 18
  • 30
  • Have you tried it? Did it work? If not, what was the error? – Mark Byers Feb 20 '12 at 15:20
  • 1
    So what problems are you having? Have you tried this code? Does it work, are there problems with it? Are you simply concerned about design or security types of issues? – Servy Feb 20 '12 at 15:20
  • Sorry I should of been more specific the error states "The system cannot find the file specified." I keep finding other peoples code on the net when they know the set path and the program they want to access. I want it to be more dynamic than that. However Havn't found anyone that wants to do what I am doing - Which I find odd. – Marshal Feb 20 '12 at 15:21
  • 2
    Never just concatenate paths, always look for a better way, e.g. [`Path.Combine`](http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx). – Brad Christie Feb 20 '12 at 15:22

3 Answers3

3

Yes it will work, but I would recommend you update your code this instead:

var path = Path.Combine(p1, l1);
Process.Start(path);
Philip Fourie
  • 111,587
  • 10
  • 63
  • 83
  • This works as I have just tested it in a textbox however it will not load the file. I have a feeling it's something to do with spaces in the filename and the .doc extension has been taken off. So still having the same error. – Marshal Feb 20 '12 at 15:28
  • Strange the `Path.GetFileName` should not remove the extension. Neither should the spaces be an issue. – Philip Fourie Feb 20 '12 at 15:32
  • Just tested the code on my machine. Opened a Word doc from my desktop and it worked as expected. The path also contained spaces. – Philip Fourie Feb 20 '12 at 15:45
  • Thank you it was my error in the end a bit of code I took out stopped it from working properly. Thank you for your help - What a releif was ready to tear my hair out. – Marshal Feb 20 '12 at 15:51
2

You shouldn't use string concatenation to combine a directory and filename. In your case the resulting string will look like this:

C:\documents and settings\documentsfilename
                                  ^^
                             this is wrong

Instead use Path.Combine.

string path = Path.Combine(p1, l1);
Process.Start(path);
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

Why don't you simply do this: -

public void button4_Click(object sender, EventArgs e)
{
    string fileNameWithPath;
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        fileNameWithPath = openFileDialog1.FileName;
    }
}

public void button2_Click(object sender, EventArgs e)
{
    Process.Start(fileNameWithPath);
}
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38