推薦:.NET嵌入IronPython交互詳解隨著IronPyhon 2.0 的發(fā)布,.NET Dynamic Language Runtime 也更加成熟了,在2.0中我們可以用動(dòng)態(tài)腳本以粘合劑的方式編寫架構(gòu)體系中的各種邏輯單元,既便于修改,又能靈活適合多變的業(yè)務(wù)場(chǎng)景。當(dāng)然,我的目標(biāo)是在 Platform Framework 中能嵌入腳本引擎,而不
第一次寫使用.NET C# 開(kāi)發(fā)了一個(gè)稍稍像樣子的軟件,在這個(gè)軟件開(kāi)發(fā)過(guò)程中我查了好多資料,也學(xué)到了很多小技巧像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();
-
-
- df.Description = "選擇所有數(shù)據(jù)庫(kù)文件所在根目錄地址";
-
-
- df.ShowNewFolderButton = false;
-
-
-
-
- if (tBoxDbRoot.Text != "")
- {
- df.SelectedPath = tBoxDbRoot.Text;
- }
- else
- {
- df.RootFolder = Environment.SpecialFolder.MyComputer;
- }
-
- DialogResult result = df.ShowDialog();
- if (result == DialogResult.OK)
- {
-
- 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ù)文件備份目錄";
-
- if (Cls_dbBackRootPath != "")
- {
- bakFolder.SelectedPath = Cls_dbBackRootPath;
- }
- else
- {
- bakFolder.RootFolder = Environment.SpecialFolder.MyComputer;
- }
- if (bakFolder.ShowDialog(this) == DialogResult.OK)
- {
- Cls_dbBackRootPath = bakFolder.SelectedPath;
-
- }
二、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);
- if (curFolderRoot.Exists)
- {
-
- }
2.創(chuàng)建目錄使用Create()方法
C#代碼
- DirectoryInfo curFolderRoot = new DirectoryInfo(Cls_dbRootPath);
- if (curFolderRoot.Exists)
- {
- curFolderRoot.Create()
- }
四、FileInfo(文件信息) 獲取文件信息、復(fù)制、刪除文件等,將指定文件夾下的符合條件的文件的相關(guān)信息依次寫入DataGridView控件。
1.獲取文件信息代碼:
C#代碼
- FileInfo dbFile = new FileInfo(dbPath);
-
- 寫入DataGridView控件的某行某列上
- dGrideFileList.Rows[rowsNum].Cells[1].Value = dbFile.Length;
-
- 修改時(shí)間寫入
- dGrideFileList.Rows[rowsNum].Cells[5].Value = dbFile.LastWriteTime.ToString();
2.檢測(cè)文件是否存在執(zhí)行刪除復(fù)制操作
C#代碼
- FileInfo copyFile = new FileInfo(copyToPath);
- 檢測(cè)文件是否存在
- if (copyFile.Exists)
- {
-
- File.Delete(copyToPath);
- }
- 執(zhí)行文件的復(fù)制操作
- File.Copy(dbPath, copyToPath);
五、DataGridView(數(shù)據(jù)表格控件)用于顯示、更新、刪除等對(duì)數(shù)據(jù)列表的操作
1.將遍歷符合要求的數(shù)據(jù)添加到控件
C#代碼
- filesTotelSize += curDbFile.Length;
-
-
- string[] fileInfoArr = new string[]{
- curDbFile.FullName.Replace(Cls_dbRootPath,"").ToString(),
- CheckFile.FormatSize(curDbFile.Length),
- "0",
- "未壓縮",
- CheckFile.GetTypeName(filePath),
- curDbFile.LastWriteTime.ToString()
- };
-
-
- 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#代碼
-
- 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)目錄地址
-
-
-
-
- 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
軟件介紹與資源下載:
批量壓縮ACCESS數(shù)據(jù)庫(kù)工具 V 1.1.0版
批量壓縮多站點(diǎn)下的ACCESS數(shù)據(jù)庫(kù)
分享:如何使ASP.NET 避免頁(yè)面重新整理時(shí)重復(fù)送出有些使用者的行為真是令人猜不透,開(kāi)網(wǎng)頁(yè)有事沒(méi)事就來(lái)給你 Refresh 一下,這個(gè)動(dòng)作看似無(wú)害,但是在剛執(zhí)行過(guò) Submit 的情況下,Refresh 網(wǎng)頁(yè)會(huì)造成重復(fù)執(zhí)行,這也是為什么在各大購(gòu)物網(wǎng)站的交易付款動(dòng)作,都會(huì)提示「不要關(guān)閉網(wǎng)頁(yè)或重新整理避免造成交易失敗或