简体   繁体   English

从 Windows Forms 应用程序执行批处理文件

[英]Executing a batch file from Windows Forms application

I have written a small batch file that copies an exe from solution to the system32 folder.我写了一个小的批处理文件,将一个 exe 从解决方案复制到system32文件夹。

copy "blah.exe" "%systemroot%/System32"

The batch file works fine and copies the exe if ran from the desktop by double clikcing (placed exe on the desktop as well)如果通过双击从桌面运行,批处理文件工作正常并复制 exe(也将 exe 放在桌面上)

However, I tried doing that from Windows Application by:但是,我尝试通过以下方式从 Windows 应用程序执行此操作:

Process.Start("sample.bat");

(EXE file and batfile -> Properties -> Output to Copy Always) The cmd window does come up, but the.exe file is not there in the destination directory. (EXE 文件和 batfile -> 属性 -> Output 始终复制) cmd window 确实出现了,但 .exe 文件不在目标目录中。 What am I missing here?我在这里错过了什么?

in your batch file change path to that particular folder where you have blah.exe, change to the particular drive and then to particular folderlets say your source folder is C:\test then type cd\test in the batch file, it should be something like:在你的批处理文件中更改路径到你有 blah.exe 的那个特定文件夹,更改到特定驱动器然后到特定 folderlets 说你的源文件夹是 C:\test 然后在批处理文件中键入 cd\test,它应该是什么像:

C:
cd\test
copy "blah.exe" "%systemroot%/System32"

or use copy with complete path eg或使用具有完整路径的副本,例如

copy "C:\test\blah.exe" "%systemroot%/System32"

EDIT: To copy using CMD try:编辑:要使用 CMD 进行复制,请尝试:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new       System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b blah.exe %systemroot%/System32";
process.StartInfo = startInfo;
process.Start();

Edit 2: Or for batch file编辑 2:或用于批处理文件

 System.Diagnostics.Process.Start("cmd", "/c sample.bat");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM