C# 文件操作函數 創(chuàng)建文件 判斷存在
更新時間:2016年05月25日 11:04:41 作者:秦風
本文列舉了C#中文件操作中常用的函數,創(chuàng)建文件和判斷文件存不存在的基本使用,簡單實用,希望能幫到大家。
文件創(chuàng)建:
復制代碼 代碼如下:
File.Create(Application.StartupPath + "\\AlarmSet.txt");//創(chuàng)建該文件
System.IO.Path.GetFileName(filePath) //返回帶擴展名的文件名
System.IO.Path.GetFileNameWithoutExtension(filePath) //返回不帶擴展名的文件名
System.IO.Path.GetDirectoryName(filePath) //返回文件所在目錄
System.IO.Path.GetFileName(filePath) //返回帶擴展名的文件名
System.IO.Path.GetFileNameWithoutExtension(filePath) //返回不帶擴展名的文件名
System.IO.Path.GetDirectoryName(filePath) //返回文件所在目錄
文件存在:
復制代碼 代碼如下:
File.Exists(Application.StartupPath + "\\AlarmSet.txt")
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(Application.StartupPath + "\\AlarmSet.txt");
MessageBox.Show(info.Exists.ToString());
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(Application.StartupPath + "\\AlarmSet.txt");
MessageBox.Show(info.Exists.ToString());
是不是很簡單,但有時候我們可能對GetFileNameWithoutExtension或GetDirectoryName這樣的函數,感覺比較陌生,以前還總傻傻的自己寫函數實現,原來就一行代碼搞定啊~
相關文章
C# 字符串string和內存流MemoryStream及比特數組byte[]之間相互轉換
本文主要介紹字符串string和內存流MemoryStream及比特數組byte[]之間相互轉換的方法,需要的小伙伴可以參考一下。2016-05-05