C#
//檔案是否存在 bool result = System.IO.File.Exists(檔案路徑); //資料夾是否存在 bool result = System.IO.Directory.Exists(資料夾路徑); //建立資料夾 System.IO.Directory.CreateDirectory(資料夾路徑); //檢查是否為合法路徑 public bool isValidPath(string path) { bool result = true; string err = ""; try { System.IO.FileInfo info = new System.IO.FileInfo(path); } catch (Exception ex) { result = false; err = "err:" + ex.Message; } Console.WriteLine("{0} {1} {2}", result, path, err); return result; } //取得資料夾資訊 DirectoryInfo dir = new DirectoryInfo(資料夾路徑); //搜尋資料夾1 DirectoryInfo[] dirList = dir.GetDirectories(搜尋字串, SearchOption); //搜尋資料夾2 string[] dirList = Directory.GetDirectories(資料夾路徑, 搜尋字串, SearchOption); //搜尋檔案1 FileInfo[] fileList = dir.GetFiles(搜尋字串, SearchOption); //搜尋檔案2 string[] fileList = Directory.GetFiles(資料夾路徑, 搜尋字串, SearchOption); //巡覽DirectoryInfo foreach (DirectoryInfo _event in dirList) { //... } //巡覽FileInfo foreach (FileInfo _event in fileList) { //... } //取得目前使用者桌面路徑 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string filePath =desktopPath +@"\FolderTest\"; //檢查資料夾是否存在 if (!Directory.Exists(filePath)) { //不存在則建立 Directory.CreateDirectory(filePath); } //檢查檔案是否存在 if (!File.Exists(filePath)) { //不存在則拷貝 File.Copy(來源路徑含檔名, 目的路徑含檔名, true); }
VB
'檔案是否存在 FileIO.FileSystem.FileExists(檔案路徑) '資料夾是否存在 FileIO.FileSystem.DirectoryExists(資料夾路徑) '建立資料夾 FileIO.FileSystem.CreateDirectory(資料夾路徑) '檢查是否為合法路徑 Public Function isValidPath(ByVal path As String) As Boolean Dim result As Boolean = True Dim err As String = "" Try Dim info As New System.IO.FileInfo(path) Catch ex As Exception result = False err = "err:" & ex.Message End Try Console.WriteLine("{0} {1} {2}", result, path, err) Return result End Function '取得資料夾資訊 Dim dir As New DirectoryInfo(資料夾路徑) '搜尋資料夾1 Dim dirList As DirectoryInfo() = dir.GetDirectories(搜尋字串, SearchOption) '搜尋資料夾2 Dim dirList As String() = Directory.GetDirectories(資料夾路徑, 搜尋字串, SearchOption) '搜尋檔案1 Dim fileList As FileInfo() = dir.GetFiles(搜尋字串, SearchOption) '搜尋檔案2 Dim fileList As String() = Directory.GetFiles(資料夾路徑, 搜尋字串, SearchOption) '巡覽DirectoryInfo For Each _event As DirectoryInfo In dirList '... Next '巡覽FileInfo For Each _event As FileInfo In fileList '... Next
文章標籤
全站熱搜
留言列表