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

本人在前几天做了一道题如下(在116行中用(int)cki.KeyChar==26解决了C#中在控制台捕捉Ctrl+Z):
解决的方法也是请教了老师,经老师调试过才得出的解决方法.( 因在ConsoleKey的枚举中无Ctrl此键)
总结的心得是,单步调试方法确实是有效解决问题的路径.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace 用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 */

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

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

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

45 }

46 }

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

54 }

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

62 }

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

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

77 }

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

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

89
90 //实现Copy命令的方法
91 private 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(new char[] { ' ' });
97 string inputFileText = "";
98
99 if (strs.Length > 3 || strs[0] != "copy")//输入命令字符串不合法
100 {
101 Console.WriteLine("您输入的copy命令不正确!!");
102 return;
103 }

104
105 else
106 {
107 if (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]);
113 while (true)
114 {
115 cki = Console.ReadKey();
116 if (cki.Key == ConsoleKey.End||(int)cki.KeyChar==26) //只实现了按End建就结束并写入文件
117 { //但Ctrl+Z还没实现
118 break;
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]);
126 return;
127 }

128 //********????????????????????????????????????????????????????????????????***********
129 else
130 {
131 if (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();
139 return;
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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值