C#解決在控制台中輸入Ctrl+Z的問題

來源:互聯網
上載者:User

本人在前幾天做了一道題如下(在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是複製命令,不多解釋。
9 con 是dos 裝置檔案的簡稱。 在dos中把很多外部裝置作為檔案,稱為裝置檔案。
10 dos中這樣規定的:con 控制台(鍵盤/顯示器) aux (或com1)第一個串口 lpt1 第一個並行印表機介面,
11 nul 不存在的裝置
12  所以,舉例說明:
13 copy con abc.txt
14 這條命令的意思就是從鍵盤中把輸入的文字複製到檔案abc.txt中去,所以輸入命令後,在輸入字元,結束時按下
15 ctrl+z.你輸入的文字就會儲存到abc.txt這個檔案裡了。
16 而如果你輸入的是
17 copy 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("\x001A"),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}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.