简体   繁体   English

如何使用C#在控制台应用程序中执行命令或进程

[英]How to Execute a command or process in Console application using C#

I am developing one console application that will convert all spss (.sav) files to .csv files. 我正在开发一个控制台应用程序,它将所有spss(.sav)文件转换为.csv文件。 For this I created a SPSS job (spssJob1.spj) manually (using one .sps file) and I am iterating through all the input files (all .sav files) and trying to run that job by updating the input and output path in the .sps file (text.sps). 为此,我手动创建了一个SPSS作业(spssJob1.spj)(使用一个.sps文件),然后遍历所有输入文件(所有.sav文件),并尝试通过更新输入和输出路径来运行该作业。 .sps文件(text.sps)。 But I don't know how to call that job execution command from my app. 但是我不知道如何从我的应用程序调用该作业执行命令。

Currently, the command is: 当前,命令为:

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

and this should be executed from 这应该从执行

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

because this stats command will be available only in this directory. 因为此stats命令仅在此目录中可用。

So in my app I need to call this process from this path; 因此,在我的应用中,我需要从此路径调用此过程; I am able to call one .exe file by using my app but I don't know how to call one command form a specific directory. 我可以使用我的应用程序来调用一个.exe文件,但是我不知道如何从特定目录中调用一个命令。

This is my code: 这是我的代码:

// 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();
    }
}
        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);

you can do this by altering ProcessStartInfo's properties. 您可以通过更改ProcessStartInfo的属性来实现。

Not sure what stats is, if its an exe then you can specify the full exe path and just omit working directory. 不确定什么是统计信息,如果它是exe,则可以指定完整的exe路径,而只是省略工作目录。

  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();

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

相关问题 我如何在 c# 控制台应用程序中执行 CMD 命令? - how i can execute CMD command in c# console application? 如何使用命令中的开关在 C# 控制台应用程序中运行命令? - How to run a command in C# Console Application using switches in the command? 如何使用C#在内存中执行.NET控制台应用程序? - How to execute a .NET console-application in memory using C#? 使用命令提示符中的参数执行C#控制台应用程序 - Execute a C# console application with parameters in command prompt 使用C#使用相同的进程执行多个命令行 - Execute multiple command lines with the same process using C# 在控制台应用程序c#中使用time hour执行查询 - execute query using time hour in console application c# 如何在 C# Windows 控制台应用程序中执行命令? - How to execute command in a C# Windows console app? 使用.bat文件执行C#控制台应用程序(.exe) - Execute C# console application(.exe) using .bat file 如何检查进程是否结束,然后在C#ProcessStartInfo中执行下一个命令? - How to check if the process ended and then execute the next command in C# ProcessStartInfo? 如何使用“schtasks”执行计划任务而无需使用C#进程打开新的命令行窗口? - How to execute a scheduled task with “schtasks” without opening a new command line window using C# process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM