WinForm可以执行控制台程序吗?如果是这样,怎么样?

时间:2021-07-13 00:03:28

I have a free command line tool called FW Tools. Works great. Here is a sample line i enter in the console window :-

我有一个名为FW Tools的免费命令行工具。效果很好。以下是我在控制台窗口中输入的示例行: -

ogr2ogr -f "ESRI Shapefile" -a_srs "EPSG:26986" -t_srs "EPSG:4326"
    towns_geodetic.shp TOWNSSURVEY_POLY.shp

I wish to change the last to arguments, based on some list i dynamically generate (i use Linq-to-Filesystem and grab all the filenames) and then call this program 'n' times.

我希望改变最后的参数,基于我动态生成的一些列表(我使用Linq-to-Filesystem并获取所有文件名),然后'n'次调用该程序。

I don't care about the output.

我不关心输出。

Can this be done?

可以这样做吗?

This is all under the .NET environment btw.

这完全是在.NET环境下。

EDIT

Is there also any way to make sure the code waits for the process to finish?

是否还有任何方法可以确保代码等待进程完成?

3 个解决方案

#1


I would take a bit more of a detailed approach to this and do something like this

我会采取更详细的方法,并做这样的事情

string ApplicationName = "ogr2ogr";
string BaseOptions = "-f \"ESRI Shapefile\" -a_srs \"EPSG:26986\" 
                      -t_srs \"EPSG:4326\"";

//Start your loop here
ProcessStartInfo oStartInfo = new ProcessStartInfo()
oStartInfo.FileName = ApplicationName;
oStartInfo.Arguments = BaseOptions + MyLoopLoadedItemsHere;
Process.Start(oStartInfo)

//If you want to wait for exit, uncomment next line
//oStartInfo.WaitForExit();

//end your loop here

Something like this is a bit more readable, at least in my opinion.

至少在我看来,这样的东西更具可读性。

#2


Use Process.Start. Something like ...

使用Process.Start。就像是 ...

Process.Start( "cmd /c Gregory -f \"ES RI Shape file\" 
      -a_Sirs \"PEGS:26986\" -t_Sirs \"PEGS:4326\"
      towns_geodetic.Shep TOWNS SURVEY_PLOY.Shep" );

Here are some examples of how to do it a bit cleaner.

以下是一些如何更清洁的例子。

#3


Here we go my friend

我们去了我的朋友

Process.Start(@"C:\Windows\notepad.exe", @"C:\Windows\system.ini");

or

System.Diagnostics.Process.Start(@"C:\Windows\notepad.exe", 
                                 @"C:\Windows\system.ini");

#1


I would take a bit more of a detailed approach to this and do something like this

我会采取更详细的方法,并做这样的事情

string ApplicationName = "ogr2ogr";
string BaseOptions = "-f \"ESRI Shapefile\" -a_srs \"EPSG:26986\" 
                      -t_srs \"EPSG:4326\"";

//Start your loop here
ProcessStartInfo oStartInfo = new ProcessStartInfo()
oStartInfo.FileName = ApplicationName;
oStartInfo.Arguments = BaseOptions + MyLoopLoadedItemsHere;
Process.Start(oStartInfo)

//If you want to wait for exit, uncomment next line
//oStartInfo.WaitForExit();

//end your loop here

Something like this is a bit more readable, at least in my opinion.

至少在我看来,这样的东西更具可读性。

#2


Use Process.Start. Something like ...

使用Process.Start。就像是 ...

Process.Start( "cmd /c Gregory -f \"ES RI Shape file\" 
      -a_Sirs \"PEGS:26986\" -t_Sirs \"PEGS:4326\"
      towns_geodetic.Shep TOWNS SURVEY_PLOY.Shep" );

Here are some examples of how to do it a bit cleaner.

以下是一些如何更清洁的例子。

#3


Here we go my friend

我们去了我的朋友

Process.Start(@"C:\Windows\notepad.exe", @"C:\Windows\system.ini");

or

System.Diagnostics.Process.Start(@"C:\Windows\notepad.exe", 
                                 @"C:\Windows\system.ini");