C # basic notes (15th days ),
1. Directory
// File
Path
FileStream StreamReader StreamWriter stream
Directory
// Create a folder
Directory. CreateDirectory (@ "C: \ a \ B ");
Console. WriteLine ("created successfully ");
Console. ReadKey ();
// Delete a folder. If the folder is not empty, an exception is thrown. Add a folder to the folder. true: Delete the folder forcibly.
Directory. Delete (@ "c: \ a", true );
Console. WriteLine ("deleted successfully ");
Console. ReadKey ();
// Cut the folder
Directory. Move (@ "c: \ a", @ "C: \ Users \ SJD \ Desktop \ new ");
Console. WriteLine ("Cut successful ");
Console. ReadKey ()
// Obtain the full path of all objects in the specified folder and return a string array.
String [] path = Directory. GetFiles (@ "C: \ Users \ SJD \ Desktop \ picture ");
For (int I = 0; I <path. Length; I ++)
{
Console. WriteLine (path [I]);
}
Console. ReadKey ();
// Obtain the full path of all the images in the specified folder and return a string Array (*. format, no exception will be thrown if none exist)
String [] path = Directory. GetFiles (@ "C: \ Users \ SJD \ Desktop \ picture", "*. jpg ");
For (int I = 0; I <path. Length; I ++)
{
Console. WriteLine (path [I]);
}
Console. ReadKey ();
// Obtain the paths of all folders in the specified folder
String [] path = Directory. GetDirectories (@ "C: \ Users \ SJD \ Desktop \ picture ");
For (int I = 0; I <path. Length; I ++)
{
Console. WriteLine (path [I]);
}
Console. ReadKey ();
// Determine whether the specified folder exists and create 100 subdirectories under the Directory
If (Directory. Exists (@ "C: \ Users \ SJD \ Desktop \ new "))
{
For (int I = 0; I <100; I ++)
{
Directory. CreateDirectory (@ "C: \ Users \ SJD \ Desktop \ new \" + I );
}
}
Console. WriteLine ("OK ");
Console. ReadKey ();
Directory operation folder
CreateDirectory
Delete folder
Move cut folder
Exists
GetFiles obtains the full path of all objects in the specified folder directory.
GetDirectory obtains the full path of all folders in the specified folder directory.
2. browser control WebBrowser
Url
String text = textBox1.Text;
Uri uri = new Uri ("http: //" + text );
WebBrowser1.Url = uri;
3. ComboBox drop-down box Control
DropDownStyle: controls the appearance style of the drop-down box
Name: cbo +...
Private void Form1_Load (object sender, EventArgs e)
{
// Add the Year to the drop-down box when the program is loaded
// Obtain the current year
Int year = DateTime. Now. Year;
For (int I = year; I >= 1949; I --)
{
CboYear. Items. Add (I + "year ");
}
}
/// <Summary>
/// Load the month when the year changes
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void cboYear_SelectedIndexChanged (object sender, EventArgs e)
{
// You should clear the previous content before adding
CboMoon. Items. Clear ();
For (int I = 1; I <= 12; I ++)
{
CboMoon. Items. Add (I + "month ");
}
}
/// <Summary>
/// Load the day when the month changes
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void cboMoon_SelectedIndexChanged (object sender, EventArgs e)
{
CboDay. Items. Clear ();
Int day = 0; // defines a variable to store days
// Obtain the month
String strMoon = cboMoon. SelectedItem. ToString (). Split (new char [] {'month'}, StringSplitOptions. RemoveEmptyEntries) [0];
String strYear = cboYear. SelectedItem. ToString (). Split (new char [] {'Year'}, StringSplitOptions. RemoveEmptyEntries) [0];
// MessageBox. Show (cboMoon. SelectedItem. ToString ());
Int year = Convert. ToInt32 (strYear );
Int moon = Convert. ToInt32 (strMoon );
Switch (moon)
{
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12: day = 31;
Break;
Case 2:
If (year % 4 = 0 & amp; year % 100! = 0) | (year % 400 = 0 ))
{
Day = 29;
}
Else
{
Day = 28;
}
Break;
Default: day = 30;
Break;
}
For (int I = 1; I <= day; I ++)
{
CboDay. Items. Add (I + "");
}
}
4. Click to change the image
1) when loading a program, read all the image file names in the specified image folder to listBox.
5. Scissors
Stone 1 scissors 2 cloth 3
Players win: 1-2 =-1 2-3 =-1 3-1 = 2
Flat: subtraction = 0
Another case: The computer won.
You must return a number to me for comparison.
Concept
1. Create 6 labels (text) and 3 buttons on form1)
Label1 (show players :) label2 (Name changed to lblPlayer) label3 (show computers :) label4 (Name changed to lblComputer ))
Label5 (display referee) label6 (Name changed to lblResult (result ))
Label1 and label3 are the names label2 and label4 for players and computers.
Button1, 2, and 3 are shown as stones, scissors, and cloth. Change their names to btnStone, btnCut, and btnBu.
2. Click stone first
Assign a stone value to a string 'str '.
Assign the str value to the player. When you click the "Stone" button, let the stone appear behind the player's. LblPlayer. text = str ;()
3. How to Make a player play a game
Create a class Player
Create a method for guessing in the class, place it in the string type, and return the Int type (which is determined based on the number subtraction) public int ShowFist (string fist)
First, create a num of the int type, assign 0 to the initial value, and return num;
Create the switch-case type in the middle to determine the int value returned by the three forms of boxing.
Switch (fist)
{
Case "Stone": num = 1;
Break;
Case "Scissors": num = 2;
Break;
Case "cloth": num = 3;
Break;
}
4. How to play games on a computer
Create a Computer class
The method used to create a guess in the class is the same as that used by the player, but the string type value public int ShowFist () is not required ()
First, create a random number random r = new random ();
Limit the random number of r to 1-3 and assign the value to an Int type rNumber int rNumber = r. Next );
There is no such thing as computer stone, scissors, or cloth.
Therefore, you need to declare a field or attribute separately to store things and assign them to lblComputer.
Create an automatic attribute to store the fist
Public string Fist
{
Get;
Set;
}
Use switch-case to judge random values 1, 2, and 3, and assign stones, scissors, and cloth to this. Fist.
Switch (rNumber)
{
Case 1: this. Fist = "Stone ";
Break;
Case 2: this. Fist = "Scissors ";
Break;
Case 3: this. Fist = "cloth ";
Break;
}
Finally, return the rNumber of the int type and subtract it from the player to determine the return rNumber.
5. Make a method to determine whether to win or lose
Create a class CaiPan
Create a static class to save the step of creating object new, return 3 results, win, lose, flatten. Returns string, int, and so on. To review enumeration, use enumeration.
Create an enumeration and write the three results
Public enum Result
{
Players win,
Computer win,
Draw
}
Static class written as public static Result Judge (int playerNumber, int computerNumber)
Return the enumerated type of a Result. Input int type players and computer numbers to subtract and determine
Use If to judge
Public static Result Judge (int playerNumber, int computerNumber)
{
If (playerNumber-computerNumber =-1 | playerNumber-computerNumber = 2)
{
Return Result. The Player wins;
}
Else if (playerNumber-computerNumber = 0)
{
Return Result. Draw;
}
Else
{
Return Result. Computer win;
}
}
6. Return the method of clicking the stone
Create a Player Object, call the player's method to play the game, input the string type str, and return an Int type player number.
Player player = new Player ();
Int playerNumber = player. ShowFist (str );
Create another computer object, call the computer game method, and return an Int-type computer number.
Computer cpu = new Computer ();
Int cupNumber = cpu. ShowFist ();
Assign a value to the lblComputer on the display interface to show the punch.
LblComputer. Text = cpu. Fist;
Call the static method of the caipan class to compare the result and return a res result of the result type.
Result res = CaiPan. Judge (playerNumber, cpuNumber );
Convert result res to string type and assign it to lblResult.
LblResult. Text = res. ToString ();
7. encapsulate the game into a method PlayGame, which respectively gives scissors and stones.
Encapsulation
LblPlayer. Text = str;
Player player = new Player ();
Int playerNumber = player. ShowFist (str );
Computer cpu = new Computer ();
Int cpuNumber = cpu. ShowFist ();
LblComputer. Text = cpu. Fist;
Result res = CaiPan. Judge (playerNumber, cpuNumber );
8. Complete
6. open the file dialog box
// Click the pop-up dialog box
OpenFileDialog ofd = new OpenFileDialog ();
// Set the title of the dialog box
Ofd. Title = "Please select the text file you want to open ";
// You can select multiple items in the Setting dialog box.
Ofd. Multiselect = true;
// Set the initial directory of the dialog box
Ofd. InitialDirectory = @ "C: \ Users \ SJD \ Desktop ";
// Set the file type in the dialog box
Ofd. Filter = "text | *. txt | media | *. wav | image | *. jpg | all | *.*";
// Display dialog box
Ofd. ShowDialog ();
// Obtain the path of the selected file in the open dialog box and return it to the full path of the string type.
String path = ofd. FileName;
// Return if the path is null.
If (path = "")
{
Return;
}
Using (FileStream fsRead = new FileStream (path, FileMode. OpenOrCreate, FileAccess. Read ))
{
Byte [] buffer = new byte [1024*1024*5];
// The actual number of bytes read
Int r = fsRead. Read (buffer, 0, buffer. Length );
TextBox1.Text = Encoding. Default. GetString (buffer, 0, r );
}
7. Save file dialog box
SaveFileDialog sfd = new SaveFileDialog ();
Sfd. Title = "select the path to save ";
Sfd. InitialDirectory = @ "C: \ Users \ SJD \ Desktop ";
Sfd. Filter = "text file | *. txt | all files | *.*";
Sfd. ShowDialog ();
String path = sfd. FileName;
If (path = "")
{
Return;
}
Using (FileStream fsWrite = new FileStream (path, FileMode. OpenOrCreate, FileAccess. Write ))
{
Byte [] buffer = Encoding. Default. GetBytes (textBox1.Text );
FsWrite. Write (buffer, 0, buffer. Length );
}
MessageBox. Show ("saved successfully ");
8. font and color
FontDialog fd = new FontDialog ();
Fd. ShowDialog ();
TextBox1.Font = fd. Font;
ColorDialog cd = new ColorDialog ();
Cd. ShowDialog ();
TextBox1.ForeColor = cd. Color;
9. Process
We can regard every running application in the computer as a process.
A process is composed of multiple threads.
// Obtain all running processes in the current program
Process [] pros = Process. GetProcesses ();
Foreach (var item in pros)
{
// End all processes in the current program
// Item. Kill ()
Console. WriteLine (item );
}
Console. ReadKey ();
// Open some applications through the process
Process. Start ("calc ");
Process. Start ("mspaint ");
Process. Start ("notepad ");
Process. Start ("iexplore", "http://www.baidu.com ");
// Open a specified file through a process
// Process to open the instance
ProcessStartInfo psi = new ProcessStartInfo (@ "C: \ Users \ SJD \ Desktop \ AE .txt ");
// 1: Create a process object
Process p = new Process ();
P. StartInfo = psi;
P. Start ();
10. problems caused by a single thread
A single thread (main thread) does one thing. If it has not been done, it will not be able to do another thing, and it will be suspended.
A single thread can only do a single task, but it cannot be complicated.
// Create a thread to execute this method
Thread th = new Thread (Test );
// Mark that this thread is ready and can be executed at any time. The Cpu determines when to execute this thread.
Th. Start ();
* ** Foreground thread: the program can be closed only when all foreground threads are closed.
* ** Background thread: the background thread ends automatically as long as all foreground threads end.
By default, all newly created threads are called foreground threads.
So the program must be set as a background thread.
// Set the thread to a background thread
Th. IsBackground = true;
Th. Start ();
In. Net, cross-thread access is not allowed.
The application can not check whether there is cross-thread access.
// Cancel cross-thread access
Control is the base class of all windows spaces.
Control. checkforillegalcrossthreadcils = false;
// When you click Close form to determine whether the new thread is null
If (th! = Null)
{
// End the thread and the thread cannot be started again if it is abort.
Th. abort ();
}
The CPU cannot be operated by code
Runtime ---- debugging --- window --- output