使用批处理参数运行C#
在java中,可以通过批处理将参数传递给程序。我怎么能在C#中做到这一点 例如,如果我需要程序接收文件名,如何将其发送到程序?应用程序中的Main()例程接收一个字符串数组,其中包含在命令行上传递的参数使用批处理参数运行C#,c#,batch-file,C#,Batch File,在java中,可以通过批处理将参数传递给程序。我怎么能在C#中做到这一点 例如,如果我需要程序接收文件名,如何将其发送到程序?应用程序中的Main()例程接收一个字符串数组,其中包含在命令行上传递的参数 static void Main(string[] args) { foreach (string s in args) { Console.WriteLine(s); } Console.R
static void Main(string[] args)
{
foreach (string s in args)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
应用程序中的Main()例程接收一个字符串数组,其中包含在命令行上传入的参数
static void Main(string[] args)
{
foreach (string s in args)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
假设您已经创建了一个C#控制台应用程序(exe),它将使用接收字符串数组的主静态方法创建。这些字符串将是传递给程序的参数 例如:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Join("\n", args));
}
}
如果控制台应用程序名为“MyApp.exe”,则可以通过以下方式传递参数:
MyApp.exe“第一个参数”第二个
您应该得到以下输出:
假设您已经创建了一个C#console应用程序(exe),它将使用一个接收字符串数组的主静态方法创建。这些字符串将是传递给程序的参数 例如:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Join("\n", args));
}
}
如果控制台应用程序名为“MyApp.exe”,则可以通过以下方式传递参数:
MyApp.exe“第一个参数”第二个
您应该得到以下输出:
如果您试图读取*.bat文件的输出,这将有助于您`
Process thisProcess = new Process();
thisProcess.StartInfo.CreateNoWindow = true;
thisProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
thisProcess.StartInfo.WorkingDirectory = @"C:\Users\My User Name\Documents";
thisProcess.StartInfo.FileName = "ipconfig";
thisProcess.StartInfo.Arguments = "/h";
thisProcess.StartInfo.UseShellExecute = false;
thisProcess.StartInfo.RedirectStandardOutput = true;
thisProcess.Start();
thisProcess.WaitForExit();
//Output from the batch file
string myOutput = thisProcess.StandardOutput.ReadToEnd();
如果您试图读取*.bat文件的输出,这将有助于您`
Process thisProcess = new Process();
thisProcess.StartInfo.CreateNoWindow = true;
thisProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
thisProcess.StartInfo.WorkingDirectory = @"C:\Users\My User Name\Documents";
thisProcess.StartInfo.FileName = "ipconfig";
thisProcess.StartInfo.Arguments = "/h";
thisProcess.StartInfo.UseShellExecute = false;
thisProcess.StartInfo.RedirectStandardOutput = true;
thisProcess.Start();
thisProcess.WaitForExit();
//Output from the batch file
string myOutput = thisProcess.StandardOutput.ReadToEnd();
在Main之外,您可以使用
Environment.GetCommandLineArgs()
在Main之外,您可以使用
Environment.GetCommandLineArgs()
您可以在main方法中使用args,也可以使用app.config。您将如何处理批处理?是否要将批处理文件的输出重定向到C程序?删除
[java]
标记作为答案与Java无关。您可以在main方法中使用args,也可以使用app.config。您将如何处理批处理?是否要将批处理文件的输出重定向到C程序?删除[Java]
标记作为答案与Java无关。