简体   繁体   中英

Delete file in windows 7 using VB.NET

I have written following code in vb.net to delete the file.

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")

This code works perfectly in windows xp. But in Windows 7,I can not delete it. This OS is hectic OS from developer's point of view. Some or other problem occurs and Microsoft has not considered the developer while building this OS.

How do I delete file in Windows 7 ?

It's so easy to do so;

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

Have a nice day!

You don't need to delete the file: there is an overload File.Copy Method (String, String, Boolean) which allows overwriting.

You didn't say what error you get. I suspect it is because the user doesn't have write access to the directory. You should probably be using a subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or maybe .LocalApplicationData , and definitely not the directory containing the program.

Also, using Path.Combine(strPath, "ReportEng.ini") is how you're meant to combine paths - it'll take care of, eg, the trailing path separator for you.

The preferred method for interfacing with the Windows file system uses the following namespace:

Imports Microsoft.VisualBasic.FileIO.FileSystem

To delete a file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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