下面給出了使用C# 開(kāi)發(fā)的一個(gè)壓縮ACCESS數(shù)據(jù)庫(kù)的程序
像FolderBrowserDialog(用于瀏覽選擇文件夾的對(duì)話框)、MessageBox(消息處理對(duì)話框)、DirectoryInfo(目錄信息,可用于創(chuàng)建、檢測(cè)是否存在等對(duì)目錄的操作)、FileInfo(文件信息,可用于文件的檢測(cè)、文件信息的獲取、復(fù)制等操作)、DataGridView(數(shù)據(jù)表格控件,用于顯示文件信息列表數(shù)據(jù))、DataRowView(對(duì)一些數(shù)據(jù)源信息進(jìn)行篩選,排序)、System.Diagnostics.Process.Start(啟動(dòng)其它程序打開(kāi)文件夾目錄),下面就依次介紹一下在此軟件開(kāi)發(fā)中我都使用到以上控件、對(duì)象的哪些內(nèi)容。
一、FolderBrowserDialog(文件夾瀏覽對(duì)話框),在此軟件中用于打開(kāi)選擇數(shù)據(jù)庫(kù)根目錄或打開(kāi)創(chuàng)建、選擇備份目錄,下面是兩處位置的代碼詳細(xì)介紹。
1.選擇數(shù)據(jù)庫(kù)目錄,在此處不需要新建文件夾,因此屏蔽新建文件夾按鈕。
C#代碼
- FolderBrowserDialog df = new FolderBrowserDialog();
-
- //設(shè)置文件瀏覽對(duì)話框上的描述內(nèi)容
- df.Description = "選擇所有數(shù)據(jù)庫(kù)文件所在根目錄地址";
-
- //不顯示對(duì)話框下方的創(chuàng)建新文件夾按鈕
- df.ShowNewFolderButton = false;
-
- /*
- 判斷是否已直接輸入文件夾目錄地址,如果存在則將此值賦于對(duì)話框的已選地址,這樣就可以讓對(duì)話框顯示您上次選擇或添加的目錄地址了。
- */
- if (tBoxDbRoot.Text != "")
- {
- df.SelectedPath = tBoxDbRoot.Text;
- }
- else
- {
- df.RootFolder = Environment.SpecialFolder.MyComputer;//指定對(duì)話框默認(rèn)顯示的根目錄地址 注意RootFolder的接收數(shù)據(jù)類型
- }
- //顯示文件夾對(duì)話框,并返回對(duì)話框處理結(jié)果數(shù)值
- DialogResult result = df.ShowDialog();
- if (result == DialogResult.OK) //另外一種判斷方法 if (df.ShowDialog(this) == DialogResult.OK)
- {
- //將中的數(shù)據(jù)庫(kù)目錄地址賦于類全局變量數(shù)據(jù)庫(kù)根目錄
- string folderPath = df.SelectedPath;
- if (folderPath != "")
- {
- tBoxDbRoot.Text = folderPath;
- Cls_dbRootPath = tBoxDbRoot.Text;
- }
- }
2.選擇數(shù)據(jù)庫(kù)備份目錄或創(chuàng)建新的數(shù)據(jù)庫(kù)備份目錄
C#代碼
- FolderBrowserDialog bakFolder = new FolderBrowserDialog();
- bakFolder.Description = "選擇所有數(shù)據(jù)庫(kù)文件備份目錄";
- //這里沒(méi)有設(shè)計(jì) bakFolder.ShowNewFolderButton是因?yàn)槟J(rèn)些按鈕是顯示的。
- if (Cls_dbBackRootPath != "")
- {
- bakFolder.SelectedPath = Cls_dbBackRootPath;
- }
- else
- {
- bakFolder.RootFolder = Environment.SpecialFolder.MyComputer;
- }
- if (bakFolder.ShowDialog(this) == DialogResult.OK)
- {
- Cls_dbBackRootPath = bakFolder.SelectedPath;
- //這里省略了開(kāi)始處理執(zhí)行數(shù)據(jù)庫(kù)備份的代碼...
- }
二、MessageBox(消息對(duì)話框)其實(shí)他也沒(méi)有什么好介紹的,只使用到了它的消息狀態(tài)返回執(zhí)行其它代碼和普通的消息提示顯示。
1.具有消息結(jié)果返回的處理代碼
C#代碼
- DialogResult resultNum=MessageBox.Show("數(shù)據(jù)庫(kù)文件已備份到“" + Cls_dbBackRootPath + "”,是否打開(kāi)備份目錄?", "數(shù)據(jù)庫(kù)備份成功", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
- if (resultNum == DialogResult.Yes)//判斷是否按下“是”的按鈕
- {
- openDirectoryAddress(Cls_dbBackRootPath);
- }
這里就不需要再做介紹了,看一下消息對(duì)話框的幾個(gè)參數(shù)都分別是什么
2.以不同姿態(tài)顯示的消息對(duì)話框
C#代碼
- MessageBox.Show("這里是消息的提示內(nèi)容", "消息的提示標(biāo)題",消息對(duì)話框上顯示的按鈕, 消息對(duì)話框上顯示的提示圖標(biāo));
三、DirectoryInfo(目錄信息)檢測(cè)目錄是否存在、創(chuàng)建目錄文件夾在軟件中主要用于分析并創(chuàng)建指定的文件地址字符串中各級(jí)目錄
1.檢測(cè)目錄是否存在使用Exists方法
C#代碼
- DirectoryInfo curFolderRoot = new DirectoryInfo(Cls_dbRootPath);//指定需要檢測(cè)的文件夾物理地址
- if (curFolderRoot.Exists)
- {
- //...
- }
2.創(chuàng)建目錄使用Create()方法
C#代碼
- DirectoryInfo curFolderRoot = new DirectoryInfo(Cls_dbRootPath);//指定需要檢測(cè)的文件夾物理地址
- if (curFolderRoot.Exists)
- {
- curFolderRoot.Create()
- }
四、FileInfo(文件信息) 獲取文件信息、復(fù)制、刪除文件等,將指定文件夾下的符合條件的文件的相關(guān)信息依次寫(xiě)入DataGridView控件。
1.獲取文件信息代碼:
C#代碼
- FileInfo dbFile = new FileInfo(dbPath);
-
- 寫(xiě)入DataGridView控件的某行某列上
- dGrideFileList.Rows[rowsNum].Cells[1].Value = dbFile.Length;
-
- 修改時(shí)間寫(xiě)入
- dGrideFileList.Rows[rowsNum].Cells[5].Value = dbFile.LastWriteTime.ToString();
2.檢測(cè)文件是否存在執(zhí)行刪除復(fù)制操作
C#代碼
- FileInfo copyFile = new FileInfo(copyToPath);
- 檢測(cè)文件是否存在
- if (copyFile.Exists)
- {
- //如果存在文件則執(zhí)行刪除操作
- File.Delete(copyToPath);
- }
- 執(zhí)行文件的復(fù)制操作
- File.Copy(dbPath, copyToPath);
五、DataGridView(數(shù)據(jù)表格控件)用于顯示、更新、刪除等對(duì)數(shù)據(jù)列表的操作
1.將遍歷符合要求的數(shù)據(jù)添加到控件
C#代碼
- filesTotelSize += curDbFile.Length;
-
- //將文件信息寫(xiě)入字符串?dāng)?shù)組
- string[] fileInfoArr = new string[]{
- curDbFile.FullName.Replace(Cls_dbRootPath,"").ToString(),
- CheckFile.FormatSize(curDbFile.Length),
- "0",
- "未壓縮",
- CheckFile.GetTypeName(filePath),
- curDbFile.LastWriteTime.ToString()
- };
-
- //將文件行數(shù)組數(shù)據(jù)添加至控件行集中
- dGrideFileList.Rows.Add(fileInfoArr);
-
- //刷新控件顯示
- dGrideFileList.Refresh();
2.讓控件垂直滾動(dòng)條自動(dòng)滾動(dòng)
C#代碼
- dGrideFileList.FirstDisplayedScrollingRowIndex = i;
- dGrideFileList.Refresh();
3.光標(biāo)定位跟隨遍歷定位到控件單元格
C#代碼
- dGrideFileList.CurrentCell=dGrideFileList.Rows[i].Cells[0];
- dGrideFileList.Refresh();
4.DataRowView刪除控件選中行
C#代碼
- //刪除選中行數(shù)據(jù)
- if (this.dGrideFileList.SelectedRows.Count > 0)
- {
- DataRowView drv = dGrideFileList.SelectedRows[0].DataBoundItem as DataRowView;
- drv.Delete();
- }
六、Process啟動(dòng)Exporler.exe打開(kāi)指定物理地址文件夾
C#代碼
- #region 打開(kāi)目錄地址
- /// <summary>
- /// 打開(kāi)目錄地址
- /// </summary>
- /// <param name="dirAddress">需要打開(kāi)的文件夾目錄物理地址</param>
- private void openDirectoryAddress(string dirAddress)
- {
- DirectoryInfo dirFolder = new DirectoryInfo(dirAddress);
- if (dirFolder.Exists)
- {
- System.Diagnostics.Process.Start("explorer.exe", dirAddress);
- }
- else
- {
- MessageBox.Show("未找到需要打開(kāi)的目錄地址", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- #endregion