C# 与控制台程序交互!


 1.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Diagnostics;
 7 namespace WorkingConsole
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             ProcessStartInfo psi = new ProcessStartInfo("cmd");
14             psi.RedirectStandardOutput = true;
15             psi.RedirectStandardInput = true;
16             psi.UseShellExecute = false;
17             Process p = Process.Start(psi);
18 
19             p.StandardInput.WriteLine(@"dir c:\");
20             p.StandardInput.WriteLine(@"ver");
21             p.StandardInput.WriteLine(@"exit");
22 
23             string output = p.StandardOutput.ReadToEnd();
24             p.WaitForExit();
25 
26             Console.WriteLine(output);
27             Console.Read();
28 
29 
30         }
31 
32     }
33 }
34 

 

 

ref:http://blog.csdn.net/Javahaoshuang3394/archive/2009/01/02/3686305.aspx 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Diagnostics;
 7 namespace WorkingConsole
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Process p = new Process();
14             p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
15             p.StartInfo.Arguments = "2000";
16             p.StartInfo.FileName = "CheckLeapYear.exe";
17             p.StartInfo.RedirectStandardOutput = true;
18             p.StartInfo.RedirectStandardError = true;
19             p.StartInfo.RedirectStandardInput = true;
20 
21             p.StartInfo.UseShellExecute = false;
22 
23             try
24             {
25                 string output = string.Empty;
26                 string error = string.Empty;
27 
28                 p.Start();
29 
30                 p.StandardInput.WriteLine(@"exit");
31 
32                 output = p.StandardOutput.ReadToEnd();
33                 error = p.StandardError.ReadToEnd();
34                 p.WaitForExit();
35 
36 
37                 if (output != string.Empty)
38                     Console.WriteLine("Output:{0}", output);
39 
40                 if (error != string.Empty)
41                     Console.WriteLine("Error:{0}", error);
42 
43                 Console.Read();
44             }
45             catch (Exception ex)
46             {
47                 throw ex;
48             }
49             finally
50             {
51                 p.Close();
52             }
53 
54         }
55 
56     }
57 }
58 

 

 

 CheckLeapYear.exe code;

 1 /*
 2  * Created by BpLoveGcy.cnblogs.com
 3  * Gump Yin
 4  * Date: 2010-3-29
 5  * Time: 22:12
 6  * 
 7  * Version:
 8  * CopyRight:http://BpLoveGcy.cnblogs.com/
 9  */
10 using System;
11 
12 namespace CheckLeapYear
13 {
14     class Program
15     {
16         public static int Main(string[] args)
17         {
18             if (args.Length != 1)
19             {
20                 Console.WriteLine("Please input correct year number to check!");
21                 return 0;
22             }
23             int y;
24             if (!int.TryParse(args[0], out y))
25             {
26                 Console.WriteLine("Please input correct year number!");
27                 return 0;
28             }
29 
30             if (Year.IsLeap(y))
31             {
32                 Console.WriteLine("The {0} is leap year!", y);
33             }
34             else
35             {
36                 Console.WriteLine("The {0} is not leap year!", y);
37             }
38             return 1;
39         }
40     }
41 }

 

 

Year.IsLeap() code;

 1 /*
 2  * Created by BpLoveGcy.cnblogs.com
 3  * Gump Yin
 4  * Date: 2010-3-29
 5  * Time: 22:13
 6  * 
 7  * Version:
 8  * CopyRight:http://BpLoveGcy.cnblogs.com/
 9  */
10 using System;
11 
12 namespace CheckLeapYear
13 {
14     /// <summary>
15     /// Description of IsLeapYear.
16     /// </summary>
17     public class Year
18     {
19         public Year()
20         {
21         }
22         /// <summary>
23         /// 判定公历闰年遵循的一般规律为:四年一闰,百年不闰,四百年再闰。
24         /// 公历闰年的精确计算方法:(按一回归年365天5小时48分45.5秒)
25         /// 普通年能被4整除而不能被100整除的为闰年。 (如2004年就是闰年,1900年不是闰年)
26         /// 世纪年能被400整除而不能被3200整除的为闰年。 (如2000年是闰年,3200年不是闰年)
27         /// 对于数值很大的年份能整除3200,但同时又能整除172800则又是闰年。(如172800年是闰年,86400年不是闰年)
28         /// 
29         /// 公元前闰年规则如下:
30         /// 非整百年:年数除4余数为1是闰年,即公元前1、5、9……年;
31         /// 整百年:年数除400余数为1是闰年,年数除3200余数为1,不是闰年,年数除172800余1又为闰年,即公元前401、801……年。
32         /// </summary>
33         /// <param name="yN">年份数字</param>
34         /// <returns></returns>
35         public static bool IsLeap(int yN)
36         {
37 
38             if ((yN % 400 == 0 && yN % 3200 != 0)
39                || (yN % 4 == 0 && yN % 100 != 0)
40                || (yN % 3200 == 0 && yN % 172800 == 0))
41                 return true;
42             else
43                 return false;
44 
45         }
46     }
47 }
48 

 

 

 

 

ref:http://newhappy.blog.51cto.com/381292/135705 

posted @ 2010-03-29 23:50  Freedom  阅读(2502)  评论(0编辑  收藏  举报