繁体   English   中英

使用VB.NET在Windows 7中删除文件

[英]Delete file in windows 7 using VB.NET

我已经在vb.net中编写了以下代码来删除文件。

If File.Exists(strPath & "\ReportEng.ini") = True Then
File.SetAttributes(strPath & "\ReportEng.ini", FileAttributes.Normal)
File.Delete(strPath & "\ReportEng.ini")
End If
File.Copy("\\192.168.0.1\SAP_Shared\AddonExtra\ReportEng.ini", strPath & "\ReportEng.ini")

此代码在Windows XP中完美运行。 但是在Windows 7中,我无法删除它。 从开发人员的角度来看,该OS是繁忙的OS。 发生某些或其他问题,Microsoft在构建此OS时未考虑开发人员。

如何在Windows 7中删除文件?

这样做很容易;

If My.Computer.FileSystem.FileExists("C:\somefile.ext") Then 'Check whether file exists
        My.Computer.FileSystem.DeleteFile("C:\somefile.ext") 'Delete the file!
End If

祝你今天愉快!

您不需要删除文件:有一个允许覆盖的重载File.Copy方法(字符串,字符串,布尔值)

您没有说您遇到什么错误。 我怀疑这是因为用户没有对该目录的写权限。 您可能应该使用Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).LocalApplicationData返回的目录的子目录,并且绝对不要使用包含该程序的目录。

另外,使用Path.Combine(strPath, "ReportEng.ini")意味着您将合并路径-它会为您处理例如尾随路径分隔符。

与Windows文件系统交互的首选方法使用以下名称空间:

Imports Microsoft.VisualBasic.FileIO.FileSystem

删除文件:

Dim FileLocation As String = strPath & "\ReportEng.ini"
If Not GetDirectoryInfo(FileLocation).Exists Then
  GetFileInfo(FileLocation).Delete()
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM