开发者

System.Diagnostics.Process.Start problem "Can not find specific file"

开发者 https://www.devze.com 2022-12-20 09:41 出处:网络
Wh开发者_StackOverflow中文版en I launch following code on my computer it is works fine string target = e.Link.LinkData as string;

Wh开发者_StackOverflow中文版en I launch following code on my computer it is works fine

string target = e.Link.LinkData as string;
target = System.IO.Directory.GetCurrentDirectory() + target;
System.Diagnostics.Process.Start(target);

target - non-absolute path to file. Files is exists. on clear Virtual Machine exception "Can not find specific file" is produced.

Any suggestions?

Update

I underline that on my computer works fine. Why it doesn't works on another computer


Never concatenate paths using + operator. Use Path.Combine() instead. This might also be a source of a problem.


The first thing I would do, to try and get to the bottom of this, is to verify the exact path that is reported when it fails.

Amend your code to something like this (note I've switched to using Path.Combine as mentioned by another poster):

string target = e.Link.LinkData as string;
target = Path.Combine(System.IO.Directory.GetCurrentDirectory() + target);
var fileInfo = new FileInfo(target);
if (!fileInfo.Exists)
{
    throw new FileNotFoundException("The requested file was not found: " + fileInfo.FullName);
}
System.Diagnostics.Process.Start(target);

Run this on the system that fails. Is the fully qualified path really what you expected it to be?

If no - you've got to the cause.

If yes, then start investigating things like permissions. Does the user executing this have permission to access or execute the file, or the folder it is in?

Post back any progress...


GetCurrentDirectory() does not add the "\" character at the end of its returned path. You need to add it yourself if you do it like that.


Maybe a DLL is missing? Try loading the EXE in Dependency Walker to see if it produces any errors (on your virtual machine).

Also, the MSDN page for Process.Start says that ProcessStart can throw a FileNotFoundException if the PATH environment variable has a string containing quotes.


use procmon to see what file it is trying to open, and as pointed out already, use Path.Combine to concatenate your paths.

Could it be that you have a permissions issue on the other machine, that you don't have on your dev machine?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号