如何运行隐藏的 R 任务

本文关键字:隐藏 任务 运行 何运行 | 更新日期: 2023-09-27 18:31:45

我正在从 c# windows 窗体应用程序中调用 Rscript.exe,cmd 窗口打开几毫秒。python解决方案很简单:简单地启动pythonw.exe而不是python.exe

这是函数:

private void runr()
    {
        orig = richTextBox1.Text;
        System.IO.File.WriteAllText(@"C:'cp.R", richTextBox1.Text);
        string cmd = @"C:'cp.R";
        string args = @"";
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:'Program Files'R'R-3.2.3'bin'Rscript.exe";
        start.Arguments = string.Format("{0} {1}", cmd, args);
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.RedirectStandardError = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                StreamReader reader2 = process.StandardError;
                string err = reader2.ReadToEnd();
                richTextBox1.Text = result;
                if (!string.IsNullOrEmpty(err))
                    richTextBox1.Text += "'nError:'n" + err;
            }
        }
    }

设置start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;不起作用。

如何运行隐藏的 R 任务

ProcessStartInfo.CreateNoWindow 属性设置为 true 。这会阻止系统为新进程创建控制台窗口。