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



 
posted @   流水江湖  阅读(1278)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示