0

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();
Writwick
  • 2,133
  • 6
  • 23
  • 54
Chance Leachman
  • 207
  • 4
  • 17

2 Answers2

1

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.

mellodev
  • 1,597
  • 12
  • 20
  • Cool. Thanks. I think that might work, but the CMD shell executes way before the files are even copied to the user directory. How do I make luarun.Start(); run after the files are copied? By the way thanks for the quick response. – Chance Leachman May 19 '12 at 02:05
  • Can't help with that.. I think File.Copy blocks until it finishes, so I'm not sure why you'd experience that issue. Try checking if the file exists before you setup your Process() – mellodev May 19 '12 at 02:09
1

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");
Community
  • 1
  • 1
Writwick
  • 2,133
  • 6
  • 23
  • 54