0

I have a program that opens a file with this line of code:

Process p = new Process();
p.StartInfo.FileName = @"C:\Users\RandomUser\Documents\Rainmeter\Todo List.lnk";
p.Start();

I am getting an error that "The system cannot find the path specified" and the path IS valid for sure.

Does anyone know how to fix it?

Edit: It works perfectly fine when the file is an exe file.

Pett
  • 27
  • 1
  • 7

4 Answers4

0

Try the following

Process p = new Process(); 
p.StartInfo.FileName = @"Todo List.lnk"; 
p.StartInfo.WorkingDirectory = @"C:\Users\RandomUser\Documents\Rainmeter"; 
p.Start();

Lnk is a shortcut, You can use this function to get lnk target path

Public Shared Function GetLnkTarget(lnkPath As String) As String
Dim shl = New Shell32.Shell()
' Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath)
Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath))
Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath))
Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject)
Return lnk.Target.Path

End Function

Hadi
  • 36,233
  • 13
  • 65
  • 124
0

You will need to use either the start command or cmd /c followed by the link as argument.

Process p = new Process();
p.StartInfo.FileName = "start";
p.StartInfo.Arguments = "\"C:\\Users\\RandomUser\\Documents\\Rainmeter\\Todo List.lnk\"";
p.Start();

or

Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/c \"C:\\Users\\RandomUser\\Documents\\Rainmeter\\Todo List.lnk\"";
p.Start();

And take care of "long directory names" which need to be enclosed with two " characters.

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
0

The problem was that the program couldn't access "Program Files" (that's where the shortcut was leading to), so I reinstalled the app into "Program Files(x86)" and that did the trick.

Pett
  • 27
  • 1
  • 7
  • This is not an answer, because not everybody will be able to reinstall application just like that. This circumvents the problem, does not solve it. – αNerd Nov 12 '16 at 00:30
0

I don't know the exactly way to fix this problem... But I've tried to another device with path "Program file(x86)" It ran, but with my device with path "Program file" It didn't. If anyone can change path when we code into "Program file(x86)". Reply my comment please!!

  • This answer does not seem to answer anything. Please use the comments for that. – Luchspeter Dec 12 '21 at 15:06
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30568173) – nighthawk Dec 13 '21 at 21:15