[英]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.