C#-在程序中运行CMD命令. [英] C# - Running a CMD command within a Program.

查看:102
本文介绍了C#-在程序中运行CMD命令.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此程序中运行CMD命令时遇到麻烦.我正在使用"lua LuaSrcDiet.lua myscript.lua -o myscriptdone.lua"命令.每当我运行该程序时,它就会告诉我找不到指定的文件.我猜这是由于命令提示符在运行时不在正确的目录中.正确的目录是用户文件夹.您有什么想办法的,我可以解决.非常感谢.

I'm Having trouble with running a CMD command within this program. I'm using the "lua LuaSrcDiet.lua myscript.lua -o myscriptdone.lua" command. Whenever I run the program, it tells me it can't find the file specified. I guessing this is due to Command Prompt not being in the correct directory when ran. The correct directory is the user folder. Is there any way you can think of so I can fix this. Thanks so much.

try 
{
    File.Copy(filedir1, userPath + "/myscript.lua", true);
}
catch
{
    MessageBox.Show("There has been an problem. It may be because you need to select a Lua file to open.", "Love Compiler", MessageBoxButton.OK, MessageBoxImage.Error);
}

File.Copy("Stuff/LuaDiet/lua.exe", userPath + "/lua.exe", true);
File.Copy("Stuff/LuaDiet/LuaSrcDiet.lua", userPath + "/LuaSrcDiet.lua", true);

Process luarun = new Process();
luarun.StartInfo.WorkingDirectory = @"C:\Users\Leachman";
luarun.StartInfo.FileName = "lua LuaSrcDiet.lua myscript.lua -o myscriptdone.lua";
luarun.StartInfo.UseShellExecute = false;
luarun.StartInfo.Arguments = "/all";
luarun.StartInfo.RedirectStandardOutput = true;
luarun.StartInfo.CreateNoWindow = false;
luarun.Start();

推荐答案

只需编辑以下行:

从:

luarun.StartInfo.FileName = "lua LuaSrcDiet.lua myscript.lua -o myscriptdone.lua";
luarun.StartInfo.Arguments = "/all";

TO:

luarun.StartInfo.FileName = "lua.exe";
luarun.StartInfo.Arguments = "  LuaSrcDiet.lua myscript.lua -o myscriptdone.lua /all";

我认为这应该可行!

看到您的评论后,我意识到您应该使用AsynchronousFileCopy.

After seeing your comments, I realized that you should use AsynchronousFileCopy.

CODE:

public class AsyncFileCopier
{
    public delegate void FileCopyDelegate(string sourceFile, string destFile);

    public static void AsynFileCopy(string sourceFile, string destFile)
    {
        FileCopyDelegate del = new FileCopyDelegate(FileCopy);
        IAsyncResult result = del.BeginInvoke(sourceFile, destFile, CallBackAfterFileCopied, null);
    }

    public static void FileCopy(string sourceFile, string destFile)
    { 
        // Code to copy the file
    }

    public static void CallBackAfterFileCopied(IAsyncResult result)
    {
        // Code to be run after file copy is done
    }
}

像这样称呼它:

AsyncFileCopier.AsynFileCopy("Stuff/LuaDiet/lua.exe", userPath + "/lua.exe");

这篇关于C#-在程序中运行CMD命令.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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