?
Solved

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

Posted on 2011-10-14
21
Medium Priority
?
2,991 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:OrenRozen
21 Comments
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967650
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
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967668
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
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967671
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967677
have you try cmd.exe?
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967679
and I also tried CMD and START command before the MSG
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967700
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
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967709
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
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967795
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
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967803
is there any need to check for  msg.exe

0
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967820
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
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967823
I have no idea why the code con not find this specific file.
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967824
ok....enjoy...a lot..
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 36967832
enjoy...a lot.. ????
not at all )-:
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36968078
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
 
LVL 3

Author Comment

by:OrenRozen
ID: 36968143
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
 
LVL 7

Expert Comment

by:Rahul_Gade
ID: 36968742

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
 
LVL 3

Author Comment

by:OrenRozen
ID: 36968788
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
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36968806
right click on visual studio and run as administrator...:-)...
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 36968875
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
 
LVL 3

Accepted Solution

by:
OrenRozen earned 0 total points
ID: 36969284
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
 
LVL 3

Author Closing Comment

by:OrenRozen
ID: 36991267
found the problem and the solution
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question