C # solves the problem of entering ctrl+z in the console

Source: Internet
Author: User
Tags string to file valid

I did a question a few days ago (in 116 lines (int) cki. Keychar==26 solves the Ctrl+z in C # in the console capture):

The solution is also consulted the teacher, after the teacher debugging to come to the solution. (because there is no CTRL key in the Consolekey enumeration)

The summary of the experience is that one step debugging method is indeed a valid way to solve the problem.

1 using System;


2 using System.Collections.Generic;


3 using System.Text;


4


5 namespace DOS command with CSharp Copy_con


6 {


7//24, write a program to simulate the copy con command function in a DOS system.


8/**//*copy is a copy command, not much explained.


9 con is the abbreviation for DOS device files. In DOS, many external devices are used as files, called device files.


dos: The con console (keyboard/monitor) aux (or COM1) the first serial port LPT1 the first parallel printer interface,


nul devices that do not exist


12 So, for example:


Con abc.txt


14 The meaning of this command is to copy the input text from the keyboard to the file Abc.txt, so after entering the command, press
at the end of the input character.

ctrl+z. The text you enter will be saved to the Abc.txt file.


16 And if you're typing in a


copy abc.txt con

The
18 computer will copy the text from the Abc.txt to the screen, which is displayed


19 * *


Class Doscopycon


21 {


private String inputcommandtext= "";


23


private String Sourcefilename= "";


25


private String Targetfilename= "";


27//parametric constructor implementation, instantiating an object while assigning a command string


public Doscopycon (string inputcommandtext)


29 {


this.inputcommandtext = Inputcommandtext;


31}


32//Read-write properties Inputcommandtext implement input or read command strings


public string Inputcommandtext


34 {





36 {


return inputcommandtext;


38}


Set


40 {


if (value!= "")


42 {


This.inputcommandtext = value;


44}


45}


46}


47//Read-only property sourceFileName


public string sourceFileName


49 {


Get


51 {


return sourcefilename;


53}


54}


55//Read-only property TargetFileName


public string TargetFileName


57 {





59 {


return targetfilename;


61}


62}


63//has a parameter to execute copy con command method


public void Executecopyconcommand (string inputcommandtext)


65 {


if (inputcommandtext!= "")


67 {


this.inputcommandtext = Inputcommandtext;


69//start to implement the copy command


Donecopycon ();


71}



Else

73 {


Console.WriteLine ("**copy command cannot be empty * *");


return;


76}


77}


78//No-parameter copy con method


the public void Executecopyconcommand ()


80 {


Bayi if (Inputcommandtext = "")


82 {


Console.WriteLine ("**copy command cannot be empty * *");


return;


85}


86//start to implement the copy command


Donecopycon ();


88}


89


90//The method to implement the copy command


Donecopycon private void ()


92 {


//consolekeyinfo ckis=new consolekeyinfo (Convert.tochar ("\x001a"), Consolekey.z,false,true,false);


consolekeyinfo cki = new Consolekeyinfo ();


95


string[] STRs = Inputcommandtext.split (new char[] {'});


a string inputfiletext = "";


98


if (STRs. Length > 3 | | STRS[0]//input command string not valid


100 {


Console.WriteLine ("The Copy command you entered is incorrect!!");


102 return;


103}


104


Else


106 {


if (strs[1] = = "con")//Receive string from console write file


108 {


109//*******************


110//Because there is no value for the CTRL key in the Consolekey enumeration, note that you can use a single step debug method to see how many values you enter CTRL+Z in the console


111//In the debugging state of one-step debugging, the input ctrl+z learned that the Cki.keychar value is 26, so we can use this value to make a conversion comparison can be


112 Console.WriteLine ("Start writing a string to file {0} (press Ctrl+z or End and write):", strs[2]);


113 while (true)


114 {


Cki = Console.readkey ();


116 if (cki. Key = = consolekey.end| | (int) Cki. KEYCHAR==26)//Only implemented by end and write to file


117 {//But ctrl+z hasn't been implemented


118 break;


119}


Inputfiletext + = Cki. Keychar.tostring ();


121}


122 System.IO.StreamWriter SW = new System.IO.StreamWriter (strs[2));


123 SW. Write (Inputfiletext);


124 SW. Close ();


Console.WriteLine ("Write file {0} successful.", Strs[2]);


126 return;


127}


128//*******************


129 Else


130 {


131 if (strs[2] = = "con")//If read File command


132 {


Console.WriteLine ("Start reading file {0}", strs[1]);


134 Console.WriteLine ("Read the file successfully, the contents of this file are as follows:");


135 System.IO.StreamReader sr = new System.IO.StreamReader (strs[1));


136 Console.WriteLine (Sr. ReadToEnd ());


137 Console.WriteLine ("Read file completed.");


138 Sr. Close ();


139 return;


140}


141//The following operations are used to copy files to


//else//Start copy Operation


143//{


144//this.sourcefilename = strs[1];


145//This.targetfilename = strs[2];


146


147//if (strs[1] = strs[2])//If the duplicate of the same name is no matter


148//{


149//Console.WriteLine ("Copy!! of the same name");


//return;


151//}


152//else//start copying the name of the file


153//{


154//if (System.IO.File.Exists (strs[2))///target file exists delete


155//{


156//Console.Write (string. Format (Do you want to overwrite the file {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);//start Copying Files


162//Console.WriteLine ("copy source file {0} to target file {1} successful!", Strs[1], strs[2]);


163//}


164//}


165}


166}


167}


168}


169}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.