Click here to Skip to main content
15,919,879 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have batch script which works fine when i run it from command prompt. but its not working when i run it from C# code even though VS was in admin mode and process which was running this .bat was elevated as administrator.


Process process = new Process();
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.CreateNoWindow = true;
                psi.Verb = "runas";
                psi.FileName = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + batfile;
                psi.UseShellExecute = false;
                process.StartInfo = psi;
                process.Start();
                process.WaitForExit();


What I have tried:

I tried to run the process as admin
Posted
Updated 16-Feb-17 17:59pm
v2

System.IO.Path.GetDirectoryName returns the path without trailing back slash. So you have to use
C#
psi.FileName = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\" + batfile;


But note that there is an exception to this rule when the executable file is located in the root directory. See also the example outputs at Path.GetDirectoryName Method (String) (System.IO)[^].
 
Share this answer
 
Comments
Karthik_Mahalingam 16-Feb-17 6:06am    
5
shreyassv 16-Feb-17 7:01am    
I tried this,its not working
[no name] 16-Feb-17 7:18am    
The use your debugger to find out what "not working" means.
shreyassv 16-Feb-17 7:24am    
its launching bat file without + @"\" + and with + @"\" +, the location of bat is correct. But its failing to execute the steps inside batch script. But it works when i right click on bat file and do run as administrator.
bat file code is below


@ECHO PLEASE DO NOT CLOSE THIS WINDOW
@ECHO ********************************
@ECHO OFF

START /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc
Jochen Arndt 16-Feb-17 7:33am    
I'm sorry but "not working" can't be solved because I don't have access to your system to see what is happening.

All I can suggest is to do some tests to know what is not working:
- Check the file name, the existance of the file, and if access is allowed
- Use a simple batch file that does not require admin privileges to check if executing batch files is possible this way (however, it should be)
- Use Process.ExitCode
- Create a shell window to see what happens (use echo statements in the batch file)
- If the shell window just flashes (closes immediately), try to let it stay open (e.g. by waiting for keypress in batch file)
string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "dism.exe");
            if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
            {
                // For 32-bit processes on 64-bit systems, %windir%\system32 folder
                // can only be accessed by specifying %windir%\sysnative folder.
                system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative", "dism.exe");
            }


identifies the system architecture and uses DISM accordingly
 
Share this answer
 

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