Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello!
I am trying to run a bat file and programme an IC for an indstrl applctn.But when i run the following code i get an exception like that,

"WIN32Exception was unhandled"<br />
"Access is denied"


I will be very thankful to you if you can give me any kind of help to solve this problem.
Thank you.

Code:
C#
string result = "";
int response = 0;

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"G:\Projects\FEIN LIION\Documents Fien Lion\Documents\FEIN Akku_LiION_S3515\Software_423_303044_V0_10");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listfiles;

listfiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myoutput = listfiles.StandardOutput;
listfiles.WaitForExit(2000);

if (listfiles.HasExited)
{
    result = myoutput.ReadToEnd();
    response = listfiles.ExitCode;
}
MessageBox.Show(response.ToString());
}
Posted
Updated 24-Apr-11 18:46pm
v2
Comments
Sameera Fonseka 25-Apr-11 1:27am    
Sorry there is a silly mistake.

Some basic steps that you could try out

1) If you are writing to a resource make sure another process is not accessing the same resource (close the file if it is accidentally open in a resource file).
2) Make sure you have permissions to access the file.
3) If you are running Vista, you might need to change UAC settings to allow access to the resource (though this is not always a recommended practise).
4) You might need to be an administrator to open / edit this resource.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Apr-11 0:58am    
Valid check list, good to check, my 5.
See also my answer.
--SA
Abhinav S 25-Apr-11 1:02am    
Thank you SA.
One problem is that your path name contains blank spaces. You need to get such path names in quotation marks. Another problem: I don't see batch file name. The name "Software_423_303044_V0_10" looks wrong. It can be some different file or directory name; or a batch name, but where is ".bat"?

It should look like this:

C#
string pathName =
  @"""G:\Projects\FEIN LIION\Documents Fien Lion\Documents\FEIN Akku_LiION_S3515\MyBatchFile.bat""";
//pay attention for """ above


Absolute path is bad! How such thing is supposed to be portable even from one computer to another. The path should be absolute at the moment of the call to Process.Start but never hard-coded. Is should always be calculated based on configuration parameters, or the location of entry assembly or both. There are practically no such cases when a hard-coded file name could work.

Immediate constants is evil, in general. Use explicit constants, explicit read-only properties of, resource values or… variables, data read from configuration files. For example, using "" like you do is bad, you should use system.Empty.

Your exception can be somewhere else. What's the use of reporting exception problem if you do not provide exception stack with code line numbers, along with corresponding part of code? Use debugger to point out exact line of offending code and indicate it in your code sample.

—SA
 
Share this answer
 
Comments
Ankur\m/ 25-Apr-11 0:51am    
Nice suggestions, 5!
Sergey Alexandrovich Kryukov 25-Apr-11 0:56am    
Thank you, Ankur.
--SA
Abhinav S 25-Apr-11 1:03am    
Good point. I missed that. My 5.
Sergey Alexandrovich Kryukov 25-Apr-11 1:04am    
Thank you, Abhinav.
--SA
Sameera Fonseka 25-Apr-11 4:48am    
Dear SAKryukov ,
Thank you very much for ur knd attntn rgrdnt ths problm.
It ws ma silly mstke.But even aftr crrctn tht,i alws get zero as the final rslt of batch file executn,eventhogh there s an error occurs durn the excutn.Can anyone suggst a sltn fr ths?
Thank you.
http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx[^]

May be here you will get the exact things.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Apr-11 0:57am    
Prasanta, if you compare this article with OP's code, you will see OP already knows all that, nothing new (even though nothing wrong)...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900