Process.Start("echo", "%cd%") throws W32Exception File Not Found
Asked Answered
E

3

7

When I try to do Process.Start("echo", "%cd%") it raises a System.ComponentModel.Win32Exception: The system cannot find the file specified. When I do this manually in cmd it just works like it should. I never knew that there's a difference...

Also, when I do File.Exists(logfile.txt) (w/o path) of a file that should definitely be there, it returns false. This is btw the reason for the echo above: debugging...

This error doesn't occur on my developement machine, only on another one where I am testing on.

The Path variable looks normal. Both are WinXP. Both are running .NET 3.5

Ethicize answered 22/8, 2011 at 9:35 Comment(1)
why are you accessing the file without a path ? btw you can get the current working directory by Environment.CurrentDirectoryBrinna
S
13

Try Process.Start("cmd.exe", "/c echo %CD%")

as far as echo is not an executable but a command inside.

Saucy answered 22/8, 2011 at 9:38 Comment(1)
/C command : Executes the specified command and returns. For those wondering what that did.Brittenybrittingham
J
2

You can use System.Environment.CurrentDirectory if you want to pass the working directory of your application to cmd. AFAIK %CD% is internal to cmd, that's why Process.Start won't expand it. For ordinary environment variables you can use Environment.ExpandEnvironmentVariables.

Jame answered 22/8, 2011 at 9:41 Comment(0)
G
1

This is because echo is a console command, not an application - it isn't housed in an executable file of its own.

As for your file existence issue, if you just use the file name without the path then it will look in the working directory, this could be different to the directory containing the executing application, and completely different to where you think the files ought to be - you should fully qualify your paths after making yourself aware of the applications configuration.

Goles answered 22/8, 2011 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.