Chance Leachman
Chance Leachman

Reputation: 207

C# - Running a CMD command within a Program.

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

Upvotes: 0

Views: 425

Answers (2)

Writwick
Writwick

Reputation: 2163

Just Edit These Lines :

FROM :

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

I think this should work!

UPDATE

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

CODE from 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
    }
}

Call It Like :

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

Upvotes: 1

mellodev
mellodev

Reputation: 1607

Looks like you're trying to pass arguments in the filename field. Try setting the filename to the actual filename (lua.exe) and moving the other items to the arguments section.

Upvotes: 1

Related Questions