private void btnOpenFileDialog_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = @"C:\SeeSharp\LYH"; //設置起始路徑 openFileDialog.Title = "打開文件"; //對話框標題 openFileDialog.Filter = "JXI Vector file|*.iq|實信號|*.if"; //設置過濾器 openFileDialog.FilterIndex = 1; //默認過濾器中類型 openFileDialog.Multiselect = false; //是否容許多選 openFileDialog.RestoreDirectory = true; //再次打開是否記住上次路徑 if (openFileDialog.ShowDialog() != DialogResult.OK) return; //打開對話框,對話框結果不爲OK則返回 if (openFileDialog.Multiselect) { textBox1.Text = ""; foreach (var item in openFileDialog.FileNames) { textBox1.AppendText(item + "\r\n"); } } else { textBox1.Text = openFileDialog.FileName; } }
private void btnSaveFileDialog_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "打開文件"; //對話框標題 saveFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //設置過濾器 saveFileDialog.FilterIndex = 0; //默認過濾器中類型 saveFileDialog.RestoreDirectory = true; //保存對話框是否記憶上次打開的目錄 if (saveFileDialog.ShowDialog() != DialogResult.OK) return; //打開對話框,對話框結果不爲OK則返回 textBox1.Text = saveFileDialog.FileName; }
private void btnFolderBrowserDialog_Click(object sender, EventArgs e) { FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); folderBrowserDialog.SelectedPath = @"C:\SeeSharp\LYH\SeeSharpTools"; //設置默認路徑 folderBrowserDialog.Description = "請選擇一個文件夾"; //設置提示信息 if (folderBrowserDialog.ShowDialog() == DialogResult.OK) //打開對話框 //獲取選中結果 { textBox1.Text = folderBrowserDialog.SelectedPath; } }
static void SpecialDirectory() { string dir; //獲取當前目錄(即該進程從中啓動的目錄)的徹底限定路徑。 dir = Environment.CurrentDirectory; //備註 按照定義,若是該進程在本地或網絡驅動器的根目錄中啓動,則此屬性的值爲驅動器名稱後跟一個尾部反斜槓(如「C:/」)。 //若是該進程在子目錄中啓動,則此屬性的值爲不帶尾部反斜槓的驅動器和子目錄路徑(如「C:/mySubDirectory」)。 dir = Directory.GetCurrentDirectory(); //獲取當前目錄的上級目錄 dir = Path.GetFullPath(".."); //桌面路徑 dir = "桌面路徑:" + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); //ProgramData文件夾路徑 dir = "ProgramData文件夾路徑:" + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); //個人文檔文件夾路徑 dir = "個人文檔文件夾路徑" + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //得到當前可執行的exe文件名 dir = "Process.GetCurrentProcess().MainModule.FileName:" + Process.GetCurrentProcess().MainModule.FileName; //獲取基目錄,它由程序集衝突解決程序用來探測程序集。 dir = "AppDomain.CurrentDomain.BaseDirectory:" + AppDomain.CurrentDomain.BaseDirectory; //獲取啓動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。(如:D:/project/集團客戶短信服務端/bin/Debug) dir = "System.Windows.Forms.Application.StartupPath:" + System.Windows.Forms.Application.StartupPath; //獲取啓動了應用程序的可執行文件的路徑,包括可執行文件的名稱。 dir = "System.Windows.Forms.Application.ExecutablePath:" + System.Windows.Forms.Application.ExecutablePath; //獲取或設置包含該應用程序的目錄的名稱。 dir = "AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + AppDomain.CurrentDomain.SetupInformation.ApplicationBase; Console.ReadKey(); }
/// <summary> /// 獲取當前目錄及當前目錄第n級的父目錄 /// </summary> /// <param name="level">父目錄級別,大於等於0</param> /// <returns></returns> public static string CurrentDirectory(int level=0) { string str = Environment.CurrentDirectory; for (int i = 0; i < level; i++) str = str.Substring(0, str.LastIndexOf(@"\")); return str; }
static void pth() { string dir; string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml"; dir = "獲取文件的全路徑:" + Path.GetFullPath(filePath);//-->C:\JiYF\BenXH\BenXHCMS.xml dir = "獲取文件所在的目錄:" + Path.GetDirectoryName(filePath);//-->C:\JiYF\BenXH dir = "獲取文件的名稱含有後綴:" + Path.GetFileName(filePath);//-->BenXHCMS.xml dir = "獲取文件的名稱沒有後綴:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS dir = "獲取路徑的後綴擴展名稱:" + Path.GetExtension(filePath);//-->.xml dir = "獲取路徑的根目錄:" + Path.GetPathRoot(filePath);//-->C:\ Console.ReadKey(); }
static void Main(string[] args) { // 獲取目標目錄信息 string selectedDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); ; Console.WriteLine("Open : " + selectedDir+"\n"); DirectoryInfo root = new DirectoryInfo(selectedDir); // 從目標目錄信息中獲取目錄中文件信息 FileInfo[] fileInfo = root.GetFiles("*.txt"); Console.WriteLine("*.txt file list start..."); foreach (var file in fileInfo) { Console.WriteLine("new txt file:" + file.FullName); } // 從目標目錄信息中獲取子目錄信息 Console.WriteLine("\r\n\r\n sub dir list start..."); DirectoryInfo[] subDirList = root.GetDirectories(); foreach (var subDir in subDirList) { Console.WriteLine("find sub dir:" + subDir.FullName); } Console.WriteLine("\nDone..."); Console.Read(); }
代碼運行結果以下html
private void FileOperations() { string sourcePath = "sourcePath"; string destpath = "destpath"; //複製文件到目標路徑 File.Copy(sourcePath, destpath); //刪除制定文件 File.Delete(sourcePath); //移動文件到目標路徑【重命名的實現方法】 //sourcePath與destpath在不一樣目錄下則爲文件的移動,在同一目錄下則爲文件的重命名 File.Move(sourcePath, destpath); //打開一個文本文件*.txt ,讀取文件中數據,而後關閉該文件 File.ReadAllText(sourcePath); //建立一個文件,向其中寫入數據,若是此路徑下有同名文件則會覆蓋 //【PS:對文件進行寫入操做,若是路徑下有同名文件則會進行覆蓋,因此最好進行一次判斷,跟用戶交互一下在進行覆蓋】 File.WriteAllText(sourcePath, "要寫入文件的字符串");
}
參考 http://www.javashuo.com/article/p-fbeczkrf-ht.html