C#選擇文件、選擇文件夾、打開文件

一、選擇文件用OpenDialoghtml

OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//該值肯定是否能夠選擇多個文件
dialog.Title = "請選擇文件夾"; dialog.Filter = "全部文件(*.*)|*.*"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string file = dialog.FileName; }
Filter 屬性 賦值爲一字符串 用於過濾文件類型; 字符串說明以下: ‘|’分割的兩個,一個是註釋,一個是真的Filter,顯示出來的是那個註釋。若是要一次顯示多中類型的文件,用分號分開。 如: Open1.Filter="圖片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp"; 則過濾的文件類型爲 「|」號  右邊的 *.jpg;*.gif;*.bmp 三種類型文件,在OpenDialog/SaveDialog中顯示給用戶看的文件類型字符串則是 :「|」號  左邊的 圖片文件(*.jpg,*.gif,*.bmp)。 再如: Open1.Filter="圖像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png";

二、使用System.Windows.Forms.FolderBrowserDialog選擇文件夾async

System.Windows.Forms.FolderBrowserDialog dialog =new System.Windows.Forms.FolderBrowserDialog(); dialog.Description = "請選擇Txt所在文件夾"; if (dialog.ShowDialog()==System.Windows.Forms.DialogResult.OK ) { if (string.IsNullOrEmpty(dialog.SelectedPath)) { System.Windows.MessageBox.Show(this, "文件夾路徑不能爲空", "提示"); return; } this.LoadingText = "處理中..."; this.LoadingDisplay = true; Action<string> a = DaoRuData; a.BeginInvoke(dialog.SelectedPath,asyncCallback, a); }

三、直接打開某路徑下的文件或者文件夾ide

System.Diagnostics.Process.Start("ExpLore", "C:\\window");

 

<C#>_在窗體中打開文件

實現的代碼以下: public void openfile(int n) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "*.cs | *.cs";//設置文件後綴
            if (openfile.ShowDialog() == DialogResult.OK) { string filename = openfile.FileName; dic1.Add(n, filename); fileArr[n].Text = filename.Substring(filename.LastIndexOf("\\") + 1, filename.LastIndexOf(".") - (filename.LastIndexOf("\\") + 1)); } } 頁面中的【NO】按鈕是用來打開文件的,打開的文件是readonly權限,是不可編寫的,點擊【編輯】按鈕就能夠打開文件而且編輯,實現代碼以下: public void readfile(int btNumber, string mode)//點擊【NO】按鈕,以只讀發方式打開文件
 { int key = Convert.ToInt16(numArr[btNumber].Text) - 1; foreach (KeyValuePair<int, string> kv in dic1) { if (kv.Key == key) { System.IO.FileInfo f = new System.IO.FileInfo(kv.Value); if (mode == "ReadOnly") { f.Attributes = System.IO.FileAttributes.ReadOnly; } System.Diagnostics.Process csProcess = System.Diagnostics.Process.Start(kv.Value); } } } public void readfile(int btNumber)//點擊【編輯】按鈕,以可讀可寫發方式打開文件
 { int key = Convert.ToInt16(numArr[btNumber].Text) - 1; foreach (KeyValuePair<int, string> kv in dic1) { if (kv.Key == key) { System.IO.FileInfo f = new System.IO.FileInfo(kv.Value); f.Attributes = System.IO.FileAttributes.Normal; System.Diagnostics.Process csProcess = System.Diagnostics.Process.Start(kv.Value); } } } 在C#窗體中使用代碼實現文件的打開,用的是進程的思想,即Windows中每一個軟件都是一個進程,咱們平時在電腦中本身打開一個txt文件就是打開一個進程,在代碼中一樣能夠實現打開文件的功能。 關鍵語句就是: System.Diagnostics.Process csProcess = System.Diagnostics.Process.Start(kv.Value); 這裏的kv.Value是用鍵值對把文件名和【NO】中的序號對應起來,方便作一些讀寫操做。 在沒有設置文件的權限時,文件是不可改變的,因此以上代碼中,若是不實現 f.Attributes = System.IO.FileAttributes.ReadOnly; 文件打開後也是不能更改的,你們能夠試試。 爲了使文件可以修改,要設置成 f.Attributes = System.IO.FileAttributes.Normal; 設置文件的屬性主要用到了FileInfo類的Attributes屬性。
View Code

C# OpenFileDialog打開文件對話框(詳解)

1、打開文件對話框(OpenFileDialog)

一、 OpenFileDialog控件的基本屬性

  • InitialDirectory:對話框的初始目錄 
  • Filter: 獲取或設置當前文件名篩選器字符串,例如,"文本文件(*.txt)|*.txt|全部文件(*.*)||*.*" 
  • FilterIndex 在對話框中選擇的文件篩選器的索引,若是選第一項就設爲1 
  • RestoreDirectory 控制對話框在關閉以前是否恢復當前目錄 
  • FileName:第一個在對話框中顯示的文件或最後一個選取的文件 
  • Title 將顯示在對話框標題欄中的字符 
  • AddExtension 是否自動添加默認擴展名 
  • CheckPathExists 在對話框返回以前,檢查指定路徑是否存在 
  • DefaultExt 默認擴展名 
  • DereferenceLinks 在從對話框返回前是否取消引用快捷方式 
  • ShowHelp 啓用"幫助"按鈕 
  • ValiDateNames 控制對話框檢查文件名中是否不含有無效的字符或序列

二、 OpenFileDialog控件有如下經常使用事件

FileOk 當用戶點擊"打開"或"保存"按鈕時要處理的事件 
HelpRequest 當用戶點擊"幫助"按鈕時要處理的事件post

能夠用如下代碼來實現上面這個對話框:this

private void openFileDialogBTN_Click(object sender, System.EventArgs e) { OpenFileDialog openFileDialog=new OpenFileDialog(); openFileDialog.InitialDirectory="c:\\";//注意這裏寫路徑時要用c:\\而不是c:\
    openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|全部文件|*.*"; openFileDialog.RestoreDirectory=true; openFileDialog.FilterIndex=1; if (openFileDialog.ShowDialog()==DialogResult.OK) { fName=openFileDialog.FileName; File fileOpen=new File(fName); isFileHaveName=true; richTextBox1.Text=fileOpen.ReadFile(); richTextBox1.AppendText(""); } }
View Code

三、 獲取對話框的文件名

openfiledialog.FileName //獲取或設置一個包含在文件對話框中選定的文件名字符串spa

openfiledialog.SafeFileName  //獲取選定對話框中的文件名和擴展名code

2、打開文件夾對話框(FolderBrowserDialog)

FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "請選擇文件路徑"; if (dialog.ShowDialog() == DialogResult.OK) { savePath = dialog.SelectedPath; textBox2.Text = savePath; }
View Code
相關文章
相關標籤/搜索