C# - 在程序中运行 CMD 命令
本文关键字:运行 CMD 命令 程序 | 更新日期: 2023-09-27 18:34:50
我在此程序中运行CMD命令时遇到问题。我正在使用"lua LuaSrcDiet.lua myscript.lua -o myscriptdone.lua"命令。每当我运行该程序时,它都会告诉我找不到指定的文件。我猜这是由于命令提示符在运行时不在正确的目录中。正确的目录是用户文件夹。你能想到什么办法让我解决这个问题。非常感谢。
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";
自:
luarun.StartInfo.FileName = "lua.exe";
luarun.StartInfo.Arguments = " LuaSrcDiet.lua myscript.lua -o myscriptdone.lua /all";
我认为这应该有效!
更新
看到您的评论后,我意识到您应该使用AsynchronousFileCopy
.
来自Answer of another SO Question
的代码:
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");
看起来您正在尝试在文件名字段中传递参数。尝试将文件名设置为实际文件名 (lua.exe(,并将其他项目移动到参数部分。