【问题标题】:How to Execute a command or process in Console application using C#如何使用 C# 在控制台应用程序中执行命令或进程
【发布时间】:2018-08-21 17:59:23
【问题描述】:

我正在开发一个控制台应用程序,它将所有 spss (.sav) 文件转换为 .csv 文件。为此,我手动创建了一个 SPSS 作业 (spssJob1.spj)(使用一个 .sps 文件),并且我正在遍历所有输入文件(所有 .sav 文件)并尝试通过更新.sps 文件 (text.sps)。但我不知道如何从我的应用程序中调用该作业执行命令。

目前,命令是:

stats C:\Users\10522\Desktop\spssJob1.spj -production

这应该从

执行
C:\Program Files\IBM\SPSS\Statistics\22

因为这个 stats 命令只能在这个目录中使用。

所以在我的应用程序中,我需要从这条路径调用这个过程;我可以使用我的应用程序调用一个.exe 文件,但我不知道如何从特定目录调用一个命令。

这是我的代码:

// getting all spss files from the from the input path 
FileInfo[] Files = new DirectoryInfo("D:\Input").GetFiles("*.sav");

// looping each files and calling the job
foreach (FileInfo file in Files)
{ 
    if (file.Name != "")
    {
        // updating the text.sps file for each job                       
        System.IO.File.WriteAllText("D:\Input\text.sps", string.Empty);
        System.IO.File.WriteAllText("D:\Input\text.sps", (Content for the file));

        // calling the process
        var p = new Process();
        // this code will work fine simply calling one exe
        p.StartInfo = new ProcessStartInfo((@"D:\Input\temp.exe"), "-n")
        // instead of this I need to call something like this
        // stats C:\Users\10522\Desktop\spssJob1.spj -production from this 
        // path C:\Program Files\IBM\SPSS\Statistics\22 
        {
            UseShellExecute = false
        };

        p.Start();
        p.WaitForExit();
    }
}

【问题讨论】:

    标签: c# console-application spss


    【解决方案1】:
            ProcessStartInfo pi = new ProcessStartInfo("stats");
            pi.Arguments = @"C:\Users\10522\Desktop\spssJob1.spj -production";
            pi.WorkingDirectory = @"C:\Program Files\IBM\SPSS\Statistics\22";
            pi.UseShellExecute = false;
            Process.Start(pi);
    

    您可以通过更改 ProcessStartInfo 的属性来做到这一点。

    不确定 stats 是什么,如果它是 exe,那么您可以指定完整的 exe 路径并省略工作目录。

    【讨论】:

    • 感谢您的回答,stats 是使用 SPSS 执行特定作业的命令
    • 我添加了一些额外的东西,并将答案发布下来。非常感谢您的支持。
    • 我评论了这一行“pi.UseShellExecute = false;”因为当我在同一个控制台中运行时它显示了一些问题,并且我添加了“p.WaitForExit();”因为 SPSS 需要许可证来执行每个作业,所以如果我并行执行它不会生成输出文件
    【解决方案2】:
      var p = new Process();
                        p.StartInfo = new ProcessStartInfo("stats")
                        {
                            //UseShellExecute = false,
                            Arguments= @"C:\Users\10522\Desktop\spssJob1.spj -production",
                            WorkingDirectory = @"C:\Program Files\IBM\SPSS\Statistics\22",
                        };
                        p.Start();
                        p.WaitForExit();
    

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      相关资源
      最近更新 更多