从C#代码执行时批处理文件不起作用 [英] Batch file not working when executed from C# code

查看:455
本文介绍了从C#代码执行时批处理文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有批处理脚本,当我从命令提示符运行它时工作正常。但是当我从C#代码运行它时它不起作用,即使VS处于管理模式,并且运行此.bat的进程被提升为管理员。





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();





我尝试过:



我试图以admin



What I have tried:

I tried to run the process as admin

推荐答案

System.IO.Path.GetDirectoryName <运行该过程/ code>返回路径而不返回斜杠。所以你必须使用

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





但请注意当可执行文件位于根目录中时,此规则有一个例外。另请参阅 Path.GetDirectoryName方法中的示例输出(字符串)(System.IO) [ ^ ]。


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");
            }





标识系统架构并相应地使用DISM



identifies the system architecture and uses DISM accordingly


这篇关于从C#代码执行时批处理文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆