korrowan
korrowan

Reputation: 583

'Path.Combine' issue

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.

Upvotes: 6

Views: 9241

Answers (4)

D J
D J

Reputation: 937

Try the following:

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

Upvotes: -1

Priyank
Priyank

Reputation: 10623

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.

Upvotes: 0

zhangz
zhangz

Reputation: 126

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

Path.Combine(path1, path2 + path3)

Upvotes: 11

SLaks
SLaks

Reputation: 887215

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.

Upvotes: 21

Related Questions