简体   繁体   中英

'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.

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.

如果path2只是一个没有扩展名的文件名,则可以使用:

Path.Combine(path1, path2 + path3)

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.

请尝试以下操作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM