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

LVL 3
OrenRozenAsked:
Who is Participating?
 
OrenRozenConnect With a Mentor Author Commented:
After some digging, I found the problem and the solution.

apparently, the problem is with the location of the msg.exe command.
On x64 systems native x86 command are located in the c:\windows\SysWOW64 folder. Also, I've found that that specific command (msg.exe) is not in the SysWOW64 folder.

So, calling that x86 command from an x86 build application on a x64 systems will return an error on that specific command. btw, the command is in that folder on 2008 x64 system. apparently, the problem is only on Windows 7 x64 systems.

Building the solution as Any CPU solved the issue.
0
 
nishant joshiTechnology Development ConsultantCommented:
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....
0
 
nishant joshiTechnology Development ConsultantCommented:
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;
                                }
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
OrenRozenAuthor Commented:
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.
0
 
nishant joshiTechnology Development ConsultantCommented:
have you try cmd.exe?
0
 
OrenRozenAuthor Commented:
and I also tried CMD and START command before the MSG
0
 
nishant joshiTechnology Development ConsultantCommented:
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?
..


0
 
OrenRozenAuthor Commented:
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.
0
 
OrenRozenAuthor Commented:
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

0
 
nishant joshiTechnology Development ConsultantCommented:
is there any need to check for  msg.exe

0
 
OrenRozenAuthor Commented:
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!!!
0
 
OrenRozenAuthor Commented:
I have no idea why the code con not find this specific file.
0
 
nishant joshiTechnology Development ConsultantCommented:
ok....enjoy...a lot..
0
 
OrenRozenAuthor Commented:
enjoy...a lot.. ????
not at all )-:
0
 
jagrut_patelCommented:
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?

0
 
OrenRozenAuthor Commented:
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.
0
 
Rahul_GadeCommented:

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
0
 
OrenRozenAuthor Commented:
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.
0
 
nishant joshiTechnology Development ConsultantCommented:
right click on visual studio and run as administrator...:-)...
0
 
OrenRozenAuthor Commented:
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.
0
 
OrenRozenAuthor Commented:
found the problem and the solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.