当前位置:编程学习 > C#/ASP.NET >>

在C#中如何使用命令行参数

namespace zz {
    class CommandLine {
        public static void Main(string[] args) {
            int ctr = 0;
            if (args.Length <= 0)
            {
                Console.WriteLine("no Command Line arguments were provied.");
                return;
            }
             else {
                 for (ctr = 0; ctr < args.Length; ctr++) {
                     Console.WriteLine("Argument {0} is {1}",ctr+1,args[ctr]);
                 }
            }
        }
    }
}
想让他可以接受args的参数,然后显示出来。各位大神帮帮忙吧! --------------------编程问答-------------------- CommandLine.exe -arg1 -arg2 -arg3 就可以了呀 --------------------编程问答-------------------- 这个要在哪里写呢,具体步骤~~ --------------------编程问答--------------------
引用 1 楼 zl373946618289 的回复:
CommandLine.exe -arg1 -arg2 -arg3 就可以了呀


引用 2 楼 u013201035 的回复:
这个要在哪里写呢,具体步骤~~

你2个是基友么  买的一手好猛 --------------------编程问答-------------------- --------------------编程问答-------------------- 启动应用程序的时候添加
例如你c盘下面的1.exe
你cmd 输入 c:\1.exe -arg1 -arg2 -arg3
然后就可以在程序的入口可以得到了
    public static void Main(string[] args) {

} --------------------编程问答-------------------- 可以直接在vs中设置参数。我的是日文版,你也应该可以看懂。 --------------------编程问答-------------------- 补充一点 在你的工程文件右键属性就会有这一页面。 --------------------编程问答--------------------
引用 3 楼 Hsuifengershi 的回复:
Quote: 引用 1 楼 zl373946618289 的回复:

CommandLine.exe -arg1 -arg2 -arg3 就可以了呀


引用 2 楼 u013201035 的回复:
这个要在哪里写呢,具体步骤~~

你2个是基友么  买的一手好猛

--------------------编程问答--------------------
引用 3 楼 Hsuifengershi 的回复:
Quote: 引用 1 楼 zl373946618289 的回复:

CommandLine.exe -arg1 -arg2 -arg3 就可以了呀


引用 2 楼 u013201035 的回复:
这个要在哪里写呢,具体步骤~~

你2个是基友么  买的一手好猛

回答错了 请指出? 难道不是这样传参的嘛? 
在项目中有很多这样的例子都是这样应用,比如程序需要启用一个外部程序转换语音
则可以调用

// 压缩MP3
              this.StartProcess(
                    Path.Combine(this.compressPath, "ffmpeg.exe"),
                    string.Format(@"-i {0} -ac 1 -ar 8000 -ab 7950 -y {1}",
                    args.Mp3FileFullName, args.Mp3FileCompressName));

private void StartProcess(string fileName, string args)
        {
            Process process = Process.Start(new ProcessStartInfo
            {
                FileName = fileName,
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardInput = false,
                RedirectStandardOutput = false,
                CreateNoWindow = true
            });

            process.WaitForExit();
            process.Close();
        }
--------------------编程问答--------------------
引用 9 楼 zl373946618289 的回复:
Quote: 引用 3 楼 Hsuifengershi 的回复:

Quote: 引用 1 楼 zl373946618289 的回复:

CommandLine.exe -arg1 -arg2 -arg3 就可以了呀


引用 2 楼 u013201035 的回复:
这个要在哪里写呢,具体步骤~~

你2个是基友么  买的一手好猛

回答错了 请指出? 难道不是这样传参的嘛? 
在项目中有很多这样的例子都是这样应用,比如程序需要启用一个外部程序转换语音
则可以调用

// 压缩MP3
              this.StartProcess(
                    Path.Combine(this.compressPath, "ffmpeg.exe"),
                    string.Format(@"-i {0} -ac 1 -ar 8000 -ab 7950 -y {1}",
                    args.Mp3FileFullName, args.Mp3FileCompressName));

private void StartProcess(string fileName, string args)
        {
            Process process = Process.Start(new ProcessStartInfo
            {
                FileName = fileName,
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardInput = false,
                RedirectStandardOutput = false,
                CreateNoWindow = true
            });

            process.WaitForExit();
            process.Close();
        }

我只会6撸的方法 代码没传参没试过 --------------------编程问答-------------------- 编译完成后,运行cmd,将路径移至完成的编译文件夹下,例如cd X:xxx\xxx\dubug\下,运行命令名,类似于ab c.exe abc def即可。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,