Code
1 const int ERROR_FILE_NOT_FOUND = 2;
2 const int ERROR_ACCESS_DENIED = 5;
3
4 /// <summary>
5 /// Open Target Folder Select dialog
6 /// </summary>
7 /// <param name="sender"></param>
8 /// <param name="e"></param>
9 /// <remarks>Author Petter Liu http://www.cnblogs.com/wintersun/ </remarks>
10 private void button1_Click(object sender, EventArgs e)
11 {
12 folderBrowserDialog2.ShowDialog();
13 txtFolderTarget.Text = folderBrowserDialog2.SelectedPath;
14 txtFolderTarget.Text = Path.Combine(txtFolderTarget.Text,Path.GetFileName(txtFolderSrc.Text));
15 if (!string.IsNullOrEmpty(txtFolderSrc.Text))
16 this.btnGo.Enabled = true;
17 }
18
19 /// <summary>
20 /// Open Source Folder Select dialog
21 /// </summary>
22 /// <param name="sender"></param>
23 /// <param name="e"></param>
24 /// <remarks>Author Petter Liu http://www.cnblogs.com/wintersun/ </remarks>
25 private void button2_Click(object sender, EventArgs e)
26 {
27 folderBrowserDialog1.ShowDialog();
28 folderBrowserDialog1.ShowNewFolderButton = false;
29 txtFolderSrc.Text=folderBrowserDialog1.SelectedPath;
30 }
31
32 /// <summary>
33 /// Execute WinRar Packet Source code
34 /// </summary>
35 /// <param name="sender"></param>
36 /// <param name="e"></param>
37 /// <remarks>Author Petter Liu http://www.cnblogs.com/wintersun/ </remarks>
38 private void button3_Click(object sender, EventArgs e)
39 {
40 string strSrcPath = txtFolderSrc.Text;
41 string strTargetPath = txtFolderTarget.Text;
42 //关键的参数,具体内容参看WinRAR帮助手册。
43 //-x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj 排除bin,obj文件夹里所有文件,以及文件本身。
44 //注意有转义字符
45 string arg = " a -r -ag\"-[YYYY-MM-DD-HH-MM-SS]\" -m5 -x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj ";
46
47 System.Diagnostics.Process ProcessRar = new System.Diagnostics.Process();
48 ProcessRar.StartInfo.FileName = "C:\\Program Files\\WinRAR\\Rar.exe";
49 ProcessRar.StartInfo.CreateNoWindow = true;
50
51 if (this.chkFileName.Checked==false)
52 arg = " a -r -m5 -x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj ";
53
54 //图形化界面要排除的文件中可以输入 "*\bin\*" "*\bin\" "*\obj\*" "*\obj\"
55 ProcessRar.StartInfo.Arguments = string.Format("{0}{1} {2}",arg,strTargetPath, strSrcPath);
56 try
57 {
58 ProcessRar.Start();
59 }
60 catch (Win32Exception ex)
61 {
62 if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
63 {
64 MessageBox.Show(ex.Message + ". Check the path.");
65 }
66
67 else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
68 {
69 MessageBox.Show(ex.Message + ". You do not have permission to print this file.");
70 }
71 }
72 if (ProcessRar.HasExited)
73 {
74 int iExitCode = ProcessRar.ExitCode;
75 string statusstr;
76 if (iExitCode == 0)
77 {
78 statusstr = iExitCode.ToString() + " Job Complete";
79 }
80 else
81 {
82 statusstr =iExitCode.ToString() + " Job Fail.";
83 }
84 MessageBox.Show(statusstr);
85 }
86 ProcessRar.Close();
87 }
1 const int ERROR_FILE_NOT_FOUND = 2;
2 const int ERROR_ACCESS_DENIED = 5;
3
4 /// <summary>
5 /// Open Target Folder Select dialog
6 /// </summary>
7 /// <param name="sender"></param>
8 /// <param name="e"></param>
9 /// <remarks>Author Petter Liu http://www.cnblogs.com/wintersun/ </remarks>
10 private void button1_Click(object sender, EventArgs e)
11 {
12 folderBrowserDialog2.ShowDialog();
13 txtFolderTarget.Text = folderBrowserDialog2.SelectedPath;
14 txtFolderTarget.Text = Path.Combine(txtFolderTarget.Text,Path.GetFileName(txtFolderSrc.Text));
15 if (!string.IsNullOrEmpty(txtFolderSrc.Text))
16 this.btnGo.Enabled = true;
17 }
18
19 /// <summary>
20 /// Open Source Folder Select dialog
21 /// </summary>
22 /// <param name="sender"></param>
23 /// <param name="e"></param>
24 /// <remarks>Author Petter Liu http://www.cnblogs.com/wintersun/ </remarks>
25 private void button2_Click(object sender, EventArgs e)
26 {
27 folderBrowserDialog1.ShowDialog();
28 folderBrowserDialog1.ShowNewFolderButton = false;
29 txtFolderSrc.Text=folderBrowserDialog1.SelectedPath;
30 }
31
32 /// <summary>
33 /// Execute WinRar Packet Source code
34 /// </summary>
35 /// <param name="sender"></param>
36 /// <param name="e"></param>
37 /// <remarks>Author Petter Liu http://www.cnblogs.com/wintersun/ </remarks>
38 private void button3_Click(object sender, EventArgs e)
39 {
40 string strSrcPath = txtFolderSrc.Text;
41 string strTargetPath = txtFolderTarget.Text;
42 //关键的参数,具体内容参看WinRAR帮助手册。
43 //-x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj 排除bin,obj文件夹里所有文件,以及文件本身。
44 //注意有转义字符
45 string arg = " a -r -ag\"-[YYYY-MM-DD-HH-MM-SS]\" -m5 -x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj ";
46
47 System.Diagnostics.Process ProcessRar = new System.Diagnostics.Process();
48 ProcessRar.StartInfo.FileName = "C:\\Program Files\\WinRAR\\Rar.exe";
49 ProcessRar.StartInfo.CreateNoWindow = true;
50
51 if (this.chkFileName.Checked==false)
52 arg = " a -r -m5 -x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj ";
53
54 //图形化界面要排除的文件中可以输入 "*\bin\*" "*\bin\" "*\obj\*" "*\obj\"
55 ProcessRar.StartInfo.Arguments = string.Format("{0}{1} {2}",arg,strTargetPath, strSrcPath);
56 try
57 {
58 ProcessRar.Start();
59 }
60 catch (Win32Exception ex)
61 {
62 if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
63 {
64 MessageBox.Show(ex.Message + ". Check the path.");
65 }
66
67 else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
68 {
69 MessageBox.Show(ex.Message + ". You do not have permission to print this file.");
70 }
71 }
72 if (ProcessRar.HasExited)
73 {
74 int iExitCode = ProcessRar.ExitCode;
75 string statusstr;
76 if (iExitCode == 0)
77 {
78 statusstr = iExitCode.ToString() + " Job Complete";
79 }
80 else
81 {
82 statusstr =iExitCode.ToString() + " Job Fail.";
83 }
84 MessageBox.Show(statusstr);
85 }
86 ProcessRar.Close();
87 }
我更喜欢最快速简便的方法就是用批处理文件,编写内容如下:
@echo off
C:
cd "C:\Program Files\WinRAR"
rar.exe a -ag"-[YYYY-MM-DD-HH-MM-SS]" -m5 -inul -x*\bin\* -x*\bin -x*\obj\* -x*\obj "H:\My Project\Work\XXX2\xx.rar" "H:\My Project\XXx\xx"
但上面一切的操作的前提是必须安装WinRar,有兴趣的同仁可以自行完善界面和功能,可以举一反三实现zip,7z等格式。C:
cd "C:\Program Files\WinRAR"
rar.exe a -ag"-[YYYY-MM-DD-HH-MM-SS]" -m5 -inul -x*\bin\* -x*\bin -x*\obj\* -x*\obj "H:\My Project\Work\XXX2\xx.rar" "H:\My Project\XXx\xx"
-----------------------------------------------------------------------------
http://www.cnblogs.com/wintersun/