VBNET 文件信息和目錄管理(判斷,創建,刪除,移動,復制)


 

1、判斷文件/目錄是否存在

        Try
            ' 先判斷文件是否存在。
            If Not File.Exists(TextBox4.Text) Then
                File.CreateText(TextBox4.Text) '單純創建文件一般不常用,正常情況下是創建文件然后進行讀寫操作 
                'System.IO.File.Create(TextBox4.Text)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

 

        Try
            ' 先建立目錄以便用於后續的刪除示范。
            ' If Not Directory.Exists("D:\網易") Then
            If Not Directory.Exists(TextBox5.Text) Then
                Directory.CreateDirectory(TextBox5.Text) '如果目錄不存在則創建目錄
            End If

            ' 啟動 Windows 資源管理器。
            Process.Start("explorer.exe", TextBox5.Text)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

 

 

2、創建、復制、移動、刪除文件

     2.1 獲取文件信息(創建時間) FileDateTime(fii(i).FullName)     File.GetCreationTime(path)

 System.IO.File.Create(Path)'創建文件
 System.IO.File.CreateText(Path)'創建文件
 System.IO.File.Copy(Path,targetPath)      '復制到新位置,不允許覆蓋現有文件        也可以'FileCopy(TextBox4.Text, "C:" & "\" & file_name(UBound(file_name)))
 System.IO.File.Move(SourceFileName, DestFileName)
 System.IO.File.Delete(Path)
 
'追加
 System.IO.File.AppendText
'替換
 System.IO.File.Replace

3、創建、移動、刪除目錄

        Directory.CreateDirectory(Path) '創建目錄
        Directory.Move(str1, str2)'移動目錄
        Directory.Delete(Path)'刪除目錄

 

更多操作可以查看(VS支持自動補全功能挺好的)

System.IO之File

https://msdn.microsoft.com/zh-cn/library/system.io.file(v=vs.110).aspx

https://blog.csdn.net/G00d_Boy/article/details/81627647



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM