繁体   English   中英

Windows窗体应用程序和控制台应用程序

[英]Windows Form Application with Console Application

我有一个控制台应用程序,它在启动时会询问SourcePath 。当我输入Source Path时,会询问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();
    }

但似乎没有任何作用

您可以像这样将参数添加到您的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);
    }
}

这可能不是您所需要的确切代码,但至少可以使您深入了解如何完成所需的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM