C#_解决在控制台中输入Ctrl+Z的问题

来源:互联网 发布:标签打印软件下载 编辑:程序博客网 时间:2024/05/20 19:46
本人在前几天做了一道题如下(在116行中用(int)cki.KeyChar==26解决了C#中在控制台捕捉Ctrl+Z):
解决的方法也是请教了老师,经老师调试过才得出的解决方法.(因在ConsoleKey的枚举中无Ctrl此键)
总结的心得是,单步调试方法确实是有效解决问题的路径.
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace 用CSharp实现DOS命令Copy_con
6{
7//24、编写一个程序,模拟DOS系统中的COPY CON命令功能。
8/**//*copy是复制命令,不多解释。
9con 是dos 设备文件的简称。 在dos中把很多外部设备作为文件,称为设备文件。
10dos中这样规定的:con 控制台(键盘/显示器) aux (或com1)第一个串口 lpt1 第一个并行打印机接口,
11nul 不存在的设备
12 所以,举例说明:
13copy con abc.txt
14这条命令的意思就是从键盘中把输入的文字复制到文件abc.txt中去,所以输入命令后,在输入字符,结束时按下
15ctrl+z.你输入的文字就会保存到abc.txt这个文件里了。
16而如果你输入的是
17copy abc.txt con
18计算机则会把abc.txt中的文字复制到屏幕上,也就是显示出来
19*/

20class DOSCopyCon
21{
22private string inputCommandtext="";
23
24private string sourceFilename="";
25
26private string targetFilename="";
27//有参构造函数实现,实例化对象的同时赋值命令字符串
28public DOSCopyCon(string inputCommandText)
29{
30this.inputCommandtext= inputCommandText;
31 }

32//读写属性InputCommandText实现输入或读取命令字符串
33public string InputCommandText
34{
35get
36{
37return inputCommandtext;
38 }

39set
40{
41if (value != "")
42{
43this.inputCommandtext= value;
44 }

45 }

46 }

47//只读属性SourceFileName
48public string SourceFileName
49{
50get
51{
52return sourceFilename;
53 }

54 }

55//只读属性TargetFileName
56public string TargetFileName
57{
58get
59{
60return targetFilename;
61 }

62 }

63//有参执行copy con命令方法
64public void ExecuteCopyConCommand(string inputCommandText)
65{
66if (inputCommandText!= "")
67{
68this.inputCommandtext= inputCommandText;
69//开始实现Copy命令
70 DoneCopyCon();
71 }

72else
73{
74 Console.WriteLine("**Copy命令不能为空**");
75return;
76 }

77 }

78//无参执行copy con 方法
79public void ExecuteCopyConCommand()
80{
81if (inputCommandtext== "")
82{
83 Console.WriteLine("**Copy命令不能为空**");
84return;
85 }

86//开始实现Copy命令
87 DoneCopyCon();
88 }

89
90//实现Copy命令的方法
91private void DoneCopyCon()
92{
93//ConsoleKeyInfo ckis=new ConsoleKeyInfo(Convert.ToChar("\0x001A"),ConsoleKey.Z,false,true,false);
94 ConsoleKeyInfo cki= new ConsoleKeyInfo();
95
96 String[] strs = inputCommandtext.Split(newchar[] {' ' });
97string inputFileText= "";
98
99if (strs.Length> 3|| strs[0]!= "copy")//输入命令字符串不合法
100{
101 Console.WriteLine("您输入的copy命令不正确!!");
102return;
103 }

104
105else
106{
107if (strs[1]== "con")//从控制台中接收字符串写入文件中
108{
109//********????????????????????????????????????????????????????????????????????***********
110//因ConsoleKey枚举中没有Ctrl键的值,但注意可用单步调试的方法来,查看在控制台中输入Ctrl+Z的值是多少
111//在单步调试的调试状态下,输入Ctrl+Z得知cki.KeyChar的值是26,于是我们可以利用这一值来进行转换比较即可
112 Console.WriteLine("开始往文件{0}写入字符串(按Ctrl+Z或End结束并写入):",strs[2]);
113while (true)
114{
115 cki= Console.ReadKey();
116if (cki.Key== ConsoleKey.End||(int)cki.KeyChar==26)//只实现了按End建就结束并写入文件
117{//但Ctrl+Z还没实现
118break;
119 }

120 inputFileText+= cki.KeyChar.ToString();
121 }

122 System.IO.StreamWriter sw= new System.IO.StreamWriter(strs[2]);
123 sw.Write(inputFileText);
124 sw.Close();
125 Console.WriteLine("写入文件{0}成功.", strs[2]);
126return;
127 }

128//********????????????????????????????????????????????????????????????????***********
129else
130{
131if (strs[2]== "con")//若是读文件命令
132{
133 Console.WriteLine("开始读取文件{0}",strs[1]);
134 Console.WriteLine("读取文件成功,此文件内容如下:");
135 System.IO.StreamReader sr= new System.IO.StreamReader(strs[1]);
136 Console.WriteLine(sr.ReadToEnd());
137 Console.WriteLine("读取文件已完成.");
138 sr.Close();
139return;
140 }

141//以下操作为复制文件时用到
142//else //开始复制操作
143//{
144// this.sourceFilename = strs[1];
145// this.targetFilename = strs[2];
146
147// if (strs[1] == strs[2])//若同名复制就不管了
148// {
149// Console.WriteLine("同名复制!!");
150// return;
151// }
152// else //开始复制异名文件
153// {
154// if (System.IO.File.Exists(strs[2]))//目标文件存在就删除
155// {
156// Console.Write(string.Format("您要改写文件{0}吗?(Yes/NO/All):", strs[2]));
157// string dialogResultStr = Console.ReadLine();
158// if
159// System.IO.File.Delete(strs[2]);
160// }
161// System.IO.File.Copy(strs[1], strs[2]);//开始复制文件
162// Console.WriteLine("复制源文件{0}到目标文件{1}成功!",strs[1], strs[2]);
163// }
164//}
165 }

166 }

167 }

168 }

169}

170
转载地址:http://www.cnblogs.com/ArisHuang/archive/2008/06/10/1217044.html
原创粉丝点击