带控制台应用程序的Windows窗体应用

时间:2013-01-10 09:57:50

标签: c# winforms c#-3.0 console-application

我有一个控制台应用程序在启动时要求 SourcePath ..当我输入源路径时,它要求 DestinationPath ...当我输入DestinationPath时开始执行

我的问题是通过Windows应用程序提供这些路径,意味着我需要创建一个窗口表单应用程序,它将在某个时间间隔后自动提供这些参数到控制台应用程序

是否可以实现......如果是,请求帮助......非常紧急......

喔.. 我已经尝试了很多我无法粘贴的代码,除了我用来启动应用程序的一些代码...

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe";
        psi.UseShellExecute = false;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.CreateNoWindow = false;
            psi.Arguments = input + ";" + output;
        Process p = Process.Start(psi);

        Process process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                CreateNoWindow = true,
                FileName = @"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe",
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
            }
        };
        if (process.Start())
        {
            Redirect(process.StandardError, text);
            Redirect(process.StandardOutput, text);
            MessageBox.Show(text);
        }
    private void Redirect(StreamReader input, string output)
    {
        new Thread(a =>{var buffer = new char[1];
            while (input.Read(buffer, 0, 1) > 0)
            {
                output += new string(buffer);
            };
        }).Start();
    }

但似乎没有任何工作

1 个答案:

答案 0 :(得分:0)

您可以像这样向ProcessStartInfo添加参数:

 ProcessStartInfo psi = new ProcessStartInfo(@"C:\MyConsoleApp.exe",
     @"C:\MyLocationAsFirstParamter C:\MyOtherLocationAsSecondParameter");
 Process p = Process.Start(psi);

这将启动带有2个参数的控制台应用程序。 现在在您的控制台应用程序中,您有

 static void Main(string[] args)

字符串数组args包含参数,现在您只需在应用程序启动时获取它们。

if (args == null || args.Length < 2)
{
    //the arguments are not passed correctly, or not at all
}
else
{
    try
    {
        yourFirstVariable = args[0];
        yourSecondVariable = args[1];
    }
    catch(Exception e)
    {
        Console.WriteLine("Something went wrong with setting the variables.")
        Console.WriteLine(e.Message);
    }
}

这可能是也可能不是您需要的确切代码,但至少可以让您深入了解如何实现您想要的目标。