The idea will automatically change to the same version as the one updated in A), so that more programmers can get started faster and learn more and practice more to improve their own level. Of course, I am not A cool man, there are still many shortcomings, please correct me (Declaration: there are many places in the code to be improved, for various reasons, I have not changed, complete code and some resources, please download http://blog.csdn.net/metababy from my blog ).
Project File Archive:
Http://files.cnblogs.com/meta/file sync tool .rar
Use FileSystemWatcher to search in MSDN and find the URL ms-help: // MS. VSCC.2003/MS. MSDNQTR.2003FEB. 2052/cpref/html/frlrfsystemiofilesystemwatc.
On the herclassctortopic.htm page, copy the code snippet "// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher ();
Watcher. Path = args [1];
/* Watch for changes in LastAccess and LastWrite times, and
The renaming of files or directories .*/
Watcher. policyfilter = policyfilters. LastAccess | policyfilters. LastWrite
| Policyfilters. FileName | policyfilters. DirectoryName;
// Only watch text files.
Watcher. Filter = "*. txt ";
// Add event handlers.
Watcher. Changed + = new FileSystemEventHandler (OnChanged );
Watcher. Created + = new FileSystemEventHandler (OnChanged );
Watcher. Deleted + = new FileSystemEventHandler (OnChanged );
Watcher. Renamed + = new RenamedEventHandler (OnRenamed );
// Begin watching.
Watcher. EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console. WriteLine ("Press \ 'q \ 'to quit the sample .");
While (Console. Read ()! = 'Q ');
}
// Define the event handlers.
Private static void OnChanged (object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console. WriteLine ("File:" + e. FullPath + "" + e. ChangeType );
}
Private static void OnRenamed (object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console. WriteLine ("File: {0} renamed to {1}", e. OldFullPath, e. FullPath );
}
}
"
========================
Drag FolderBrowserDialog from the toolbox to Form1. folderBrowserDialog1 will appear at the bottom of the window to keep it in the current state, Press F1, and click "Folde" on the displayed page
RBrowserDialog, the URL is ms-help: // MS. VSCC.2003/MS. MSDNQTR.2003FEB. 2052/cpref/html/frlrfsystemwindowsformsfolderb.
The rowserdialogclassctortopic.htm page, at the end of this page there is such a code snippet"
// Close the current file.
Private void closeMenuItem_Click (object sender, System. EventArgs e)
{
RichTextBox1.Text = "";
FileOpened = false;
CloseMenuItem. Enabled = false;
}
// Bring up a dialog to chose a folder path in which to open or save a file.
Private void folderMenuItem_Click (object sender, System. EventArgs e)
{
// Show the FolderBrowserDialog.
DialogResult result = folderBrowserDialog1.ShowDialog ();
If (result = DialogResult. OK)
{
FolderName = folderBrowserDialog1.SelectedPath;
If (! FileOpened)
{
// No file is opened, bring up openFileDialog in selected path.
OpenFileDialog1.InitialDirectory = folderName;
OpenFileDialog1.FileName = null;
OpenMenuItem. javasmclick ();
}
}
}
}
"
Create a C # project, select "windows application", drag the Label from the toolbox to Form1, set its location to 20, 20, size to 70, 23, and change the Text to "Source directory ". Again
Drag TextBox to Form1 and set its location to 16, 60; size to, 21; delete Text to null.
Drag the Button to Form1, set its location to, 60, size to, and change Text to "Browse ".
Add the "target directory" label (location: 20,104 size: 80, 23 text: target directory) and textbox (location: 24,152
Size: 130,192, 21 text: NULL, "Start monitoring" button (location: size: 80, 23 text: Start monitoring) and another button-"Browse"
The attributes of each component are listed as follows:
Name: label1 location: 20, 20 size: 70, 23 text: "Source directory"
Name: textbox1 location: 16, 60 size:, 21 text: NULL
Name: button1 location: 230,60 size: 63,23 text: "Browse"
Name: label2 location: 20,104 size: text: "target directory"
Name: textbox2 location: 24,152 size: 260,21 text: NULL
Name: button2 location: 130,192 size: 80, 23 text: "Start monitoring"
Name: button3 location: 232,152 size: 60, 23 text: "Browse"
In this case, form1 looks like this (Figure 1 ):
On the form1 design interface, double-click button1 To Go To The code window.
Enter the code for selecting the read directory from MSDN as described earlier in the cursor blinking position. The double-click Processing Method of button1 is as follows:
Private void button#click (object sender, System. EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog ();
If (result = DialogResult. OK)
TextBox1.Text = folderBrowserDialog1.SelectedPath;
}
Go to the form1 design interface, double-click button3, and change the double-click Processing Method of button3 to the following:
Private void button3_Click (object sender, System. EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog ();
If (result = DialogResult. OK)
TextBox2.Text = folderBrowserDialog1.SelectedPath;
}
Add the monitoring code to the processing method of button2. The code after completion is as follows:
Private void button2_Click (object sender, System. EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher ();
Watcher. Path = textBox1.Text; // The monitoring directory is changed to textbox1.text.
/* This code snippet is found in MSDN, as described earlier */
Watcher. policyfilter = policyfilters. LastAccess | policyfilters. LastWrite
| Policyfilters. FileName | policyfilters. DirectoryName;
// Only watch text files.
Watcher. Filter = "";
// Add event handlers.
Watcher. Changed + = new FileSystemEventHandler (OnChanged );
Watcher. Created + = new FileSystemEventHandler (OnChanged );
Watcher. Deleted + = new FileSystemEventHandler (OnDeleted );
Watcher. Renamed + = new RenamedEventHandler (OnRenamed );
// Begin watching.
Watcher. EnableRaisingEvents = true;
}
Note that you should add at the beginning of the Code
Using System. IO;
Using System. Diagnostics;
The two namespaces.
After processing the method of button3, add three more methods. The Code is as follows:
Private void OnChanged (object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
File. Copy (this. textBox1.Text + "\" + e. Name, this. textBox2.Text + "\" + e. Name, true );
}
Private void OnRenamed (object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
File. Copy (this. textBox1.Text + "\" + e. Name, this. textBox2.Text + "\" + e. Name, true );
}
Private void OnDeleted (object source, FileSystemEventArgs e)
{
File. Delete (this. textBox2.Text + "\" + e. Name );
}
Complete code and some resources, please visit my blog to download http://blog.csdn.net/metababy
In addition, this tool is very simple. It does not include the code for exception handling or the sub-directory function in the current directory. You can find help to see how to add the tool to MSDN.
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Using System. IO;
Using System. Diagnostics;
Namespace file synchronization Tool
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
Private System. Windows. Forms. Label label1;
Private System. Windows. Forms. TextBox textBox1;
Private System. Windows. Forms. Button button1;
Private System. Windows. Forms. Label label2;
Private System. Windows. Forms. TextBox textBox2;
Private System. Windows. Forms. Button button2;
Private System. Windows. Forms. FolderBrowserDialog folderBrowserDialog1;
Private System. Windows. Forms. Button button3;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. label1 = new System. Windows. Forms. Label ();
This. textBox1 = new System. Windows. Forms. TextBox ();
This. button1 = new System. Windows. Forms. Button ();
This. label2 = new System. Windows. Forms. Label ();
This. textBox2 = new System. Windows. Forms. TextBox ();
This. button2 = new System. Windows. Forms. Button ();
This. folderBrowserDialog1 = new System. Windows. Forms. FolderBrowserDialog ();
This. button3 = new System. Windows. Forms. Button ();
This. SuspendLayout ();
//
// Label1
//
This. label1.Location = new System. Drawing. Point (20, 20 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (70, 23 );
This. label1.TabIndex = 0;
This. label1.Text = "Source directory ";
//
// TextBox1
//
This. textBox1.Location = new System. Drawing. Point (16, 60 );
This. textBox1.Name = "textBox1 ";
This. textBox1.Size = new System. Drawing. Size (200, 21 );
This. textBox1.TabIndex = 1;
This. textBox1.Text = "";
//
// Button1
//
This. button1.Location = new System. Drawing. Point (230, 60 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (63, 23 );
This. button1.TabIndex = 2;
This. button1.Text = "Browse ";
This. button1.Click + = new System. EventHandler (this. button#click );
//
// Label2
//
This. label2.Location = new System. Drawing. Point (20,104 );
This. label2.Name = "label2 ";
This. label2.Size = new System. Drawing. Size (80, 23 );
This. label2.TabIndex = 3;
This. label2.Text = "target directory ";
//
// TextBox2
//
This. textBox2.Location = new System. Drawing. Point (24,152 );
This. textBox2.Name = "textBox2 ";
This. textBox2.Size = new System. Drawing. Size (200, 21 );
This. textBox2.TabIndex = 4;
This. textBox2.Text = "";
//
// Button2
//
This. button2.Location = new System. Drawing. Point (130,192 );
This. button2.Name = "button2 ";
This. button2.Size = new System. Drawing. Size (80, 23 );
This. button2.TabIndex = 5;
This. button2.Text = "start monitoring ";
This. button2.Click + = new System. EventHandler (this. button2_Click );
//
// Button3
//
This. button3.Location = new System. Drawing. Point (232,152 );
This. button3.Name = "button3 ";
This. button3.Size = new System. Drawing. Size (60, 23 );
This. button3.TabIndex = 6;
This. button3.Text = "Browse ";
This. button3.Click + = new System. EventHandler (this. button3_Click_1 );
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (292,273 );
This. Controls. Add (this. button3 );
This. Controls. Add (this. button2 );
This. Controls. Add (this. textBox2 );
This. Controls. Add (this. label2 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textBox1 );
This. Controls. Add (this. label1 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. ResumeLayout (false );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void button#click (object sender, System. EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog ();
If (result = DialogResult. OK)
TextBox1.Text = folderBrowserDialog1.SelectedPath;
}
Private void button2_Click (object sender, System. EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher ();
Watcher. Path = textBox1.Text; // The monitoring directory is changed to textbox1.text.
/* This code snippet is found in MSDN. The demo program created in spring has been described earlier. There are many problems that have not been solved. Please go to my blog for more information.
Http://blog.csdn.net/metababy */
Watcher. policyfilter = policyfilters. LastAccess | policyfilters. LastWrite
| Policyfilters. FileName | policyfilters. DirectoryName;
// Only watch text files.
Watcher. Filter = "";
// Add event handlers.
Watcher. Changed + = new FileSystemEventHandler (OnChanged );
Watcher. Created + = new FileSystemEventHandler (OnChanged );
Watcher. Deleted + = new FileSystemEventHandler (OnDeleted );
Watcher. Renamed + = new RenamedEventHandler (OnRenamed );
// Begin watching.
Watcher. EnableRaisingEvents = true;
}
Private void button3_Click_1 (object sender, System. EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog ();
If (result = DialogResult. OK)
TextBox2.Text = folderBrowserDialog1.SelectedPath;
}
Private void OnChanged (object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
File. Copy (this. textBox1.Text + "\" + e. Name, this. textBox2.Text + "\" + e. Name, true );
}
Private void OnRenamed (object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
File. Copy (this. textBox1.Text + "\" + e. Name, this. textBox2.Text + "\" + e. Name, true );
}
Private void OnDeleted (object source, FileSystemEventArgs e)
{
File. Delete (this. textBox2.Text + "\" + e. Name );
}
}
}