Link to home
Start Free TrialLog in
Avatar of OrenRozen
OrenRozenFlag for Israel

asked on

c# : File.Exist can not find a file in the specified folder even that it does exist

This is a strange one.

I'm using the following code to show a message to a specific user, but apparently, when running the code the msg command is reported as not found.

For testing I've changed MSG to CALC and calc is starting.
I've added if(file.exist) command to run the processinfo only if the file is found but even during debug the code is skipping the processinfo as it can not find the file.

my issue is that the file does exist and working perfectly from command line.

any ides why the code can not find this specific command?

if (File.Exists(@"c:\windows\system32\msg.exe"))
                                {
                                    ProcessStartInfo SendMsgToUser = new ProcessStartInfo("MSG", " " + Environment.UserName + " /time:10 " + "OK");
                                    SendMsgToUser.WindowStyle = ProcessWindowStyle.Hidden;
                                    Process pSendMsgToUser = Process.Start(SendMsgToUser);
                                    SendMsgToUser = null;
                                }

Open in new window

Avatar of nishant joshi
nishant joshi
Flag of India image

if (File.Exists(@"c:\windows\system32\msg.exe"))
                                {
                                    ProcessStartInfo SendMsgToUser = new ProcessStartInfo(@"c:\windows\system32\msg.exe", " " + Environment.UserName + " /time:10 " + "OK");
                                    SendMsgToUser.WindowStyle = ProcessWindowStyle.Hidden;
                                    Process pSendMsgToUser = Process.Start(SendMsgToUser);
                                    SendMsgToUser = null;
                                }



try this....
or try this.

if (File.Exists(@"c:\windows\system32\msg.exe"))
                                {
                                    ProcessStartInfo SendMsgToUser = new ProcessStartInfo("CMD.exe","MSG"+" " + Environment.UserName + " /time:10 " + "OK");
                                    SendMsgToUser.WindowStyle = ProcessWindowStyle.Hidden;
                                    Process pSendMsgToUser = Process.Start(SendMsgToUser);
                                    SendMsgToUser = null;
                                }
Avatar of OrenRozen

ASKER

already tried that and didn't work.
the reason I've added the if(file.exist... is to check and make sure the file does exist.
I've added a breakpoint  on that line and on the next step it skips the entire section as if the file does not exist.
have you try cmd.exe?
and I also tried CMD and START command before the MSG
cmd is not working for me also..

 but msg is working good....

might problem with user?



is it accessable from cmd from ur current user?
..


I'm using administrator.
and can run it from the command-line. and it is located in c:\windows\system32
what puzzles me is that even the 'if' command does not find this file.
I've create a new solution with the following code.

it always return no when looking for msg.
it returns yes for any other file like calc, notepad entc.


if (File.Exists(@"c:\windows\system32\msg.exe"))
    MessageBox.Show("yes");
else
    MessageBox.Show("no");

Open in new window

is there any need to check for  msg.exe

no. this is only for testing.

running the code for using the msg command return an exception 'The system cannot find the file specified'
but it's there!!!
I have no idea why the code con not find this specific file.
ok....enjoy...a lot..
enjoy...a lot.. ????
not at all )-:
I copy/pasted your code in my application and it worked! msg.exe, calc.exe and cmd.exe all can be found.
So I was curious to know whether I really understand this simple API File.Exists. So I visited MSDN which says,

The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Is last condition applicable in your case?

no. because I'm the administrator.
on additional tests, I copied msg.exe fform c:\windows\system32 to c:\ and set the code to look for it there. it returned yes.

I did another test and create a build of this small test solution and ran it on 2 different systems. On both it returned yes when looking on c:\windows\system32.

So, non I know the problem is only locally on my computer.

Please check this on another system, I don't think this is a coding error.
Some enviornmental factor is playing a game with you:
1. Permission to the file, if you are facing this problem while debugging the code. As your vshost process again runs under different user account.
2. Instead of hardcoding the path try to get the system dir from enviornmental variable, the actual name might have some special chars in it.
3. Try with notepad.exe before trying with your app.

-Rahul
As I already posted on my previous post, a build of the solution is working great on other systems.
So the problem is only on my local system. but not a permissions issue as I'm running administrator account with full permissions, access and owner of the folder and file.
I'll have to figure out what is "blocking" me.
right click on visual studio and run as administrator...:-)...
Thanks, but that didn't solve the issue.
didn't have any problems on the follwing systems: XP, 2003, 2008
It seams that the problem is only on Windows 7. Same issue on 2 different systems running that OS.
ASKER CERTIFIED SOLUTION
Avatar of OrenRozen
OrenRozen
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
found the problem and the solution