FileSystem.DeleteFile 方法

定义

删除文件。Deletes a file.

重载

DeleteFile(String)

删除文件。Deletes a file.

DeleteFile(String, UIOption, RecycleOption)

删除文件。Deletes a file.

DeleteFile(String, UIOption, RecycleOption, UICancelOption)

删除文件。Deletes a file.

DeleteFile(String)

删除文件。Deletes a file.

public:
 static void DeleteFile(System::String ^ file);
public static void DeleteFile (string file);
static member DeleteFile : string -> unit
Public Shared Sub DeleteFile (file As String)

参数

file
String

要删除的文件的名称和路径。Name and path of the file to be deleted.

异常

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;带有必须指定文件的尾部反斜杠;是设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; it has a trailing slash where a file must be specified; or it is a device path (starts with \\.\).

fileNothing 或空字符串。file is Nothing or an empty string.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

该文件正在使用。The file is in use.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

文件不存在。The file does not exist.

该用户没有删除该文件的权限,或者该文件为只读。The user does not have permission to delete the file or the file is read-only.

示例

此示例将删除文件 Test.txtThis example deletes the file Test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

此示例将删除 Test.txt 的文件,并允许用户确认应删除该文件。This example deletes the file Test.txt and allows the user to confirm that the file should be deleted.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

此示例将删除文件 Test.txt 并将其发送到回收站This example deletes the file Test.txt and sends it to the Recycle Bin.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.OnlyErrorDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

注解

下表列出了涉及 My.Computer.FileSystem.DeleteFile 方法的任务的示例。The following table lists an example of a task involving the My.Computer.FileSystem.DeleteFile method.

To 请参阅See
删除文件To delete a file 如何:在 Visual Basic 中删除文件How to: Delete a File in Visual Basic

安全性

FileIOPermission
控制文件和文件夹的访问权限。Controls the ability to access files and folders. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted.

DeleteFile(String, UIOption, RecycleOption)

删除文件。Deletes a file.

public:
 static void DeleteFile(System::String ^ file, Microsoft::VisualBasic::FileIO::UIOption showUI, Microsoft::VisualBasic::FileIO::RecycleOption recycle);
public static void DeleteFile (string file, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.RecycleOption recycle);
static member DeleteFile : string * Microsoft.VisualBasic.FileIO.UIOption * Microsoft.VisualBasic.FileIO.RecycleOption -> unit
Public Shared Sub DeleteFile (file As String, showUI As UIOption, recycle As RecycleOption)

参数

file
String

要删除的文件的名称和路径。Name and path of the file to be deleted.

showUI
UIOption

是否对操作进度进行可视跟踪。Whether to visually track the operation's progress. 默认值为 UIOption.OnlyErrorDialogsDefault is UIOption.OnlyErrorDialogs.

recycle
RecycleOption

删除的文件是否应发送到“回收站”。Whether or not the deleted file should be sent to the Recycle Bin. 默认值为 RecycleOption.DeletePermanentlyDefault is RecycleOption.DeletePermanently.

异常

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;带有必须指定文件的尾部反斜杠;是设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; it has a trailing slash where a file must be specified; or it is a device path (starts with \\.\).

fileNothing 或空字符串。file is Nothing or an empty string.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

该文件正在使用。The file is in use.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

文件不存在。The file does not exist.

该用户没有删除该文件的权限,或者该文件为只读。The user does not have permission to delete the file or the file is read-only.

示例

此示例将删除文件 Test.txtThis example deletes the file Test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

此示例将删除 Test.txt 的文件,并允许用户确认应删除该文件。This example deletes the file Test.txt and allows the user to confirm that the file should be deleted.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

此示例将删除文件 Test.txt 并将其发送到回收站This example deletes the file Test.txt and sends it to the Recycle Bin.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.OnlyErrorDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

注解

不是用户交互的应用程序(如 Windows 服务)不支持 showUIrecycle 参数。The showUI and recycle parameters are not supported in applications that are not user interactive, such as Windows Services.

下表列出了涉及 My.Computer.FileSystem.DeleteFile 方法的任务的示例。The following table lists an example of a task involving the My.Computer.FileSystem.DeleteFile method.

To 请参阅See
删除文件To delete a file 如何:在 Visual Basic 中删除文件How to: Delete a File in Visual Basic

安全性

FileIOPermission
控制文件和文件夹的访问权限。Controls the ability to access files and folders. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted.

UIPermission
控制与用户界面和剪贴板相关的权限。Controls the permissions related to user interfaces and the clipboard. 关联的枚举:SafeSubWindowsAssociated enumeration: SafeSubWindows.

另请参阅

DeleteFile(String, UIOption, RecycleOption, UICancelOption)

删除文件。Deletes a file.

public:
 static void DeleteFile(System::String ^ file, Microsoft::VisualBasic::FileIO::UIOption showUI, Microsoft::VisualBasic::FileIO::RecycleOption recycle, Microsoft::VisualBasic::FileIO::UICancelOption onUserCancel);
public static void DeleteFile (string file, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.RecycleOption recycle, Microsoft.VisualBasic.FileIO.UICancelOption onUserCancel);
static member DeleteFile : string * Microsoft.VisualBasic.FileIO.UIOption * Microsoft.VisualBasic.FileIO.RecycleOption * Microsoft.VisualBasic.FileIO.UICancelOption -> unit
Public Shared Sub DeleteFile (file As String, showUI As UIOption, recycle As RecycleOption, onUserCancel As UICancelOption)

参数

file
String

要删除的文件的名称和路径。Name and path of the file to be deleted.

showUI
UIOption

是否对操作进度进行可视跟踪。Whether to visually track the operation's progress. 默认值为 UIOption.OnlyErrorDialogsDefault is UIOption.OnlyErrorDialogs.

recycle
RecycleOption

删除的文件是否应发送到“回收站”。Whether or not the deleted file should be sent to the Recycle Bin. 默认值为 RecycleOption.DeletePermanentlyDefault is RecycleOption.DeletePermanently.

onUserCancel
UICancelOption

指定用户取消操作时是否引发异常。Specifies whether or not an exception is thrown when the user cancels the operation. 默认值为 UICancelOption.ThrowExceptionDefault is UICancelOption.ThrowException.

异常

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;带有必须指定文件的尾部反斜杠;是设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; it has a trailing slash where a file must be specified; or it is a device path (starts with \\.\).

fileNothing 或空字符串。file is Nothing or an empty string.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

该文件正在使用。The file is in use.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

文件不存在。The file does not exist.

该用户没有删除该文件的权限,或者该文件为只读。The user does not have permission to delete the file or the file is read-only.

用户已取消该操作并且 onUserCancel 已设置为 ThrowExceptionThe user cancelled the operation and onUserCancel is set to ThrowException.

示例

此示例将删除文件 Test.txtThis example deletes the file Test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

此示例将删除 Test.txt 的文件,并允许用户确认应删除该文件。This example deletes the file Test.txt and allows the user to confirm that the file should be deleted.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

此示例将删除文件 Test.txt 并将其发送到回收站This example deletes the file Test.txt and sends it to the Recycle Bin.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.OnlyErrorDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

注解

不是用户交互的应用程序(如 Windows 服务)不支持 showUIrecycleonUserCancel 参数。The showUI, recycle, and onUserCancel parameters are not supported in applications that are not user interactive, such as Windows Services.

下表列出了涉及 My.Computer.FileSystem.DeleteFile 方法的任务的示例。The following table lists an example of a task involving the My.Computer.FileSystem.DeleteFile method.

To 请参阅See
删除文件To delete a file 如何:在 Visual Basic 中删除文件How to: Delete a File in Visual Basic

安全性

FileIOPermission
控制文件和文件夹的访问权限。Controls the ability to access files and folders. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted.

UIPermission
控制与用户界面和剪贴板相关的权限。Controls the permissions related to user interfaces and the clipboard. 关联的枚举:SafeSubWindowsAssociated enumeration: SafeSubWindows.

另请参阅

适用于