简体   繁体   English

用C#制作批处理文件然后执行

[英]Making a batch file in C# then executing it

I'm a hobby C# programmer and I'm making an interface for a CLI tool called setMACE. 我是一名C#爱好程序员,并且正在为名为setMACE的CLI工具创建接口。 This is a snippet of the code that I'm using: 这是我正在使用的代码的片段:

if (!File.Exists(batpath))
{
    using (FileStream fs = File.Create(batpath))
    {
        fs.Close();
    }

    using (StreamWriter sw = new StreamWriter(batpath))
    {
        int outfile = r.Next(5);
        sw.WriteLine("cd " + Application.StartupPath);
        sw.WriteLine(exe + " " + "\"" + ofd.FileName + "\"" + " -d " + " >>logfile.txt");
    }
    Process proc = Process.Start(batpath);

And this will make the following .BAT: 这将产生以下.BAT:

cd C:\Users\Steve Jobs\Pictures\SetMACE_v1006
setMACE_x64.exe "C:\Users\Steve Jobs\Documents\avast.cap" -d  >>logfile.txt

This works when I execute this .BAT in the CMD but when I let the app run it, it spawns a new window with the program and ignores the >output.txt 当我在CMD中执行此.BAT时,此方法有效,但当我让应用程序运行它时,它会在程序中生成一个新窗口,而忽略>output.txt

I'm sorry if this is a no-brainer, but I can't find anything on it online. 很抱歉,这很容易,但是我在网上找不到任何东西。

It seems that you're spawning a second instance of cmd, via start, which may not be starting in the same directory to which you're cd'ing 看来您是通过start生成了cmd的第二个实例,该实例可能不在与CD相同的目录中启动

Why does your batch file have "start cmd", why don't you just run setMACE_x64.exe directly? 为什么批处理文件具有“ start cmd”,为什么不直接运行setMACE_x64.exe?

Ie your batch file would then be: 即您的批处理文件将是:

cd C:\Users\Steve Jobs\Pictures\SetMACE_v1006
setMACE_x64.exe "C:\Users\Steve Jobs\Documents\avast.cap" -d  >>logfile.txt

Try this: 尝试这个:

string str = @"/c cd C:\Users\Steve Jobs\Pictures\SetMACE_v1006";
String str2 = @"setMACE_x64.exe ""C:\Users\Steve Jobs\Documents\avast.cap"" -d  >>logfile.txt"
Process.Start("cmd.exe", str);
Process.Start("cmd.exe", str2);

I hope this will help 我希望这个能帮上忙

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

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