C#与批处理参数传递问题:
1.C#这边如何传递参数,以及接受批处理返回的参数?
2.批处理那边又如何接受C#传递过来的参数?
批处理,我也不怎么懂,请各位大哥大姐帮助下,非常感谢!

解决方案 »

  1.   

    Main方法写成如下:
    public static void Main(string[] args)
    {
    }
    这样的格式就可以使用参数了。
      

  2.   

    Main 方法可以使用参数,在这种情况下它采用下列形式之一:static int Main(string[] args)static void Main(string[] args)Main 方法的参数是表示命令行参数的 String 数组。通常通过测试 Length 属性来检查参数是否存在,例如:static void Main(string[] args){
    if (args.Length == 0)
    {
        System.Console.WriteLine("Please enter a numeric argument.");
        return 1;
    }
    }
      

  3.   

    在此示例中,程序在运行时采用一个参数,将该参数转换为整数,并计算该数的阶乘。如果没有提供参数,则程序发出一条消息来解释程序的正确用法。//计算的类
    public class Functions
    {
        public static long Factorial(int n)
        {
            if (n < 0) { return -1; }    //error result - undefined
            if (n > 256) { return -2; }  //error result - input is too big        if (n == 0) { return 1; }        // Calculate the factorial iteratively rather than recursively:        long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult;
        }
    static int Main(string[] args)
        {
            // Test if input arguments were supplied:
            if (args.Length == 0)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }        try
            {
                // Convert the input arguments to numbers:
                int num = int.Parse(args[0]);            System.Console.WriteLine("The Factorial of {0} is {1}.", num, Functions.Factorial(num));
                return 0;
            }
            catch (System.FormatException)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }
        }
    }
      

  4.   

    下面是该程序的两个运行示例(假定程序名为 Factorial.exe)。运行示例 #1:
    输入下面的命令行:Factorial 10 您将获得下面的结果:The Factorial of 10 is 3628800. 运行示例 #2:
    输入下面的命令行:Factorial 您将获得下面的结果:Please enter a numeric argument. Usage: Factorial <num> 
      

  5.   

    不好意思忘了说,我是在WINFORM里超过的
    不是Console。。
    我是根据点击WINFORM窗口的按钮,来运行批处理的!
      

  6.   

         [C#]   
    private void btnoff_Click(object sender, EventArgs e)
            {
                string path = Application.StartupPath + "\\off.bat";
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo(path,"100");//100就是参数
                p.Start();
            }        private void btncancle_Click(object sender, EventArgs e)
            {
                string path = Application.StartupPath + "\\cancle.bat";
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo(path);
                p.Start();
            }
    [/C#]
    off.bat文件:
    @echo off
    shutdown -s -t %1cancle.bat文件:
    @echo off
    shutdown -a
      

  7.   

    C# codepublic class test
    {}
      

  8.   


    没有什么区别,都是发送消息来调用的~~
    将逻辑操作写在button的事件里面即可~
      

  9.   

    非常感谢您!但是我这边还是有问题,我写了一段批处理来接受这个参数:
    if "%i"=="1"  xcopy "F:\Work\批处理\source\*.dll" "F:\Work\批处理\aaa" /e /s /y 
    if "%i"=="1"  xcopy "F:\Work\批处理\source\*.config" "F:\Work\批处理\aaa" /e /s /y
    pause
    但是这个判断根本就没用参数好像也没接受过来
    C#代码:
         p.StartInfo = new System.Diagnostics.ProcessStartInfo(str + "TEST.bat", "1");//1就是参数 
                p.Start(); 
    另外我想问下,我如果要传一个字符数组,过去怎么传?批处理那边又如何接受呢?
      

  10.   

    using System;
    using System.Diagnostics;class Test
    {
    static void Main()
    {
    Console.WriteLine("请输入控制标识:");
    string ret = Console.ReadLine();
    string path = "c:\\test.bat";
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo(path,ret);
    p.Start();
    }
    }请注意,去掉""。
    @echo off
    if %1==1 shutdown -s -t 200
    if %1==0 shutdown -a
      

  11.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;namespace Test
    {
        class Program
        {
            static void Main(string [] args)
            {
                try
                {
                    Console.WriteLine("请输入命令控制:");
                    string str = Console.ReadLine();
                    string [] execute = str.Split(' ');
                    string path =  "test.bat";
                    string order = string.Empty;
                    foreach (string s in execute)
                    {
                        order += s+" ";
                    }
                    order = order.TrimEnd(' ');
                    Process p = new Process();
                    ProcessStartInfo pi = new ProcessStartInfo(path, order);
                    p.StartInfo = pi;
                    p.Start();
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }
    }bat文件:
    @echo off   
    ::如果要调试bat命令行代码 将开关打开 修改为:@echo onif not "%1"=="1" echo 您没有选择关机if "%1"=="1" shutdown -s -t 200
    pause
    if %2==0 shutdown -a
    echo 系统已取消自动关机
    pause
    echo 下面,系统将输出第三个参数的值
    echo %3
    echo 如果第三个参数为"xcopy",系统则copy第四个参数指定的文件至第五个参数内:
    pause
    if %3==xcopy %3 %4 %5
    ::echo 复制完成
    if not "%3"=="xcopy" echo 您没有选择执行复制命令
    pause
    echo 示例结束,谢谢!
    pause楼主,再试一下。我这样子都可以的,参数直接以空格分开就行了。ProcessStartInfo的arguments只能是一个字符串。