6

Consider:

private void cmdOpenPDF_DoubleClick(object sender, EventArgs e)
{
    string path1 = @"Z:\Google Docs\Documents";
    string path2 = docIDTextBox.Text;
    string path3 = ".pdf";
    Path.Combine(path1,path2,path3);
    System.Diagnostics.Process.Start(Path.Combine(path1, path2, path3));
}

I am trying to use the above code to open a PDF file that is on the Z: drive which is a virtual drive.

When I try this I get the following:

win32 exception was unhandled:

The system cannot find the file specified

I have no idea what that means or what is wrong with my code =/. The path is valid, and I can get it to open without using the textbox.

5
  • Does path2 have the .pdf extension? You'd have the result of your path being Z:\Whatever\Foo.pdf.pdf Commented Apr 21, 2011 at 3:33
  • Next time please provide a compileable code example or a failing code example with the appropriate error. Also find a more meaningful title and don't write tags inside the title. That's what tags are for. Commented Apr 21, 2011 at 3:39
  • It does not! What happens is when someone is doing the research they download the pdf and drop it into our cloud drive. What is typed into the textbox is the document ID number provided by the City. Commented Apr 21, 2011 at 3:39
  • Path.Combine takes only two parameters how come you are giving 3. do you mean to say anithing else or this is your own class to combine path. Please post a clean code. What is the sixth line actually doing in your code. Commented Apr 21, 2011 at 3:44
  • Path2 is coming up as null (which is supposed to be the text from the text box) and hence why it cannot be found. Any idea why that would be?? Commented Apr 21, 2011 at 3:53

4 Answers 4

22

Path.Combine is used to combine multiple folders into a single path.
Therefore, your code creates the path Z:\Google Docs\Documents\something\.pdf, which is not what you want.

You should add the extension by calling Path.ChangeExtension (if you want to strip any extension from the textbox) or by simply concatenating the strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Path.ChangeExtension is exactly what I was looking for and seems cleaner and more fool-proof.
12

If path2 is only a file name without extension, you can use:

Path.Combine(path1, path2 + path3)

3 Comments

this fixes part of the issue but for some reason the value of my textbox is not getting passed. Any idea why that would be??
This works perfectly now thanks @zhangz. I figured out the other issue and my project is almost complete!
Is this the best solution Microsoft has to offer? I could just as well write string.Concat(path1, @"\", path2, path3)...
0

Check Path.Combine - Be aware of a Slash in the Second Parameter and initialize your three variables properly. Path.Combine will still work for you though it is not your best option.

Comments

-1

Try the following:

Path.Combine(path1, "\\", path2, path3);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.