c++ 删除文件_文件操作Files vb.net

File类用于VB.NET中的File操作。我们可以创建,删除,复制等操作。

如何创建文件?

为了创建一个新的File,我们可以在File类中调用Create方法。

语法:File.Create(FilePath)

FilePath:新文件对象的名称

File.Create(“ c: testFile.txt”)

如何检查文件是否存在?

在创建File对象之前,我们通常会检查File是否存在。为此,我们在File类中使用Exists方法。

语法:File.Exists(FilePath)为布尔值

FilePath:文件名

布尔值:返回true或false;如果File存在,则返回true;否则返回false

VB.NET:File.Exists(“ c: testFile.txt”)

以下VB.NET源代码显示了这些操作:

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

If File.Exists("c:estFile.txt") Then

'shows message if testFile exist

MsgBox("File 'testFile' Exist ")

Else

'create the file testFile.txt

File.Create("c:estFile.txt")

MsgBox("File 'testFile' created ")

End If

End Sub

End Class

当执行此源代码时,它首先检查File是否存在,如果存在则表明消息文件存在,否则创建一个新的File Object。

如何复制文件?

如果要复制文件对象,可以在File类中使用Copy方法。

语法:Copy(sourceFileName,destFileName)

sourceFileName:我们要移动的源文件。

destFileName:目标文件名。

VB.NET:File.Copy(“ c: testFile.txt”,“ c: testDir testFile.txt”)

如何删除文件对象?

当我们要删除文件对象时,可以使用File类中的Delete方法。

语法:Delete(FilePath)

DirPath:要删除的文件对象。

VB.NET:File.Delete(“ c: testDir testFile.txt”)

以下VB.NET源代码显示了这些操作:

Imports System.IOPublic Class Form1    Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click        If Not File.Exists("c:estFile.txt") Then            MsgBox("文件不存在 ")        Else            File.Copy("c:estFile.txt", "c:estDirestFile.txt")            MsgBox("复制文件成功 ")            File.Delete("c:estFile.txt")            MsgBox("删除文件成功")        End If    End SubEnd Class
60f6cf6e92a26a4302a7ab0d6d707179.png
Imports System.IOPublic Class Form1    Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click        If Not File.Exists("c:estFile.txt") Then            MsgBox("文件不存在 ")        Else            File.Copy("c:estFile.txt", "c:estDirestFile.txt")            MsgBox("复制文件成功 ")            File.Delete("c:estFile.txt")            MsgBox("删除文件成功")        End If    End SubEnd Class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值