Question delete read-only files and folders

d_a_r_k

Member
Joined
Oct 30, 2010
Messages
19
Programming Experience
Beginner
Hello, I searched the forum for solving this issue, but couldn't find one. I made an uninstaller which uninstalls certain application. However this application have lots of read-only files and folders and my uninstaller isn't able to remove any of them. So I need a code that would do so. Thanks in advance.

Here's the uninstaller code that I have:

VB.NET:
Imports System.IO

Dim path As String
Dim tempstring As String
Dim totalbytes As Long

Private Sub Uninstall_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        On Error Resume Next
        path = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp", "InstallLocation", "")
        If path = "" Then
            End
        End If
        totalbytes = GetFolderSize(path, True)
        IO.Directory.Delete(path & "\", True)
        My.Computer.Registry.LocalMachine.DeleteSubKey("\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp\")
        Application.DoEvents()
    End Sub

    Private Sub Uninstall_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        On Error Resume Next
        GetFileCount(My.Computer.FileSystem.SpecialDirectories.Programs & "\MyApp\")
        tempstring = "@echo off"
        tempstring = tempstring & vbCrLf & "TASKKILL /F /IM " & Chr(34) & "uninstall.exe" & Chr(34)
        tempstring = tempstring & vbCrLf & "ping localhost > nul"
        tempstring = tempstring & vbCrLf & "del " & Chr(34) & path & "\uninstall.exe" & Chr(34)
        tempstring = tempstring & vbCrLf & "del " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.Desktop & "\MyApp.lnk" & Chr(34)
        tempstring = tempstring & vbCrLf & "del " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.Programs & "\Project\" & "MyApp.lnk" & Chr(34)
        tempstring = tempstring & vbCrLf & "rmdir " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.Programs & "\Project\" & Chr(34)
        tempstring = tempstring & vbCrLf & "rmdir " & Chr(34) & path & "\" & Chr(34)
        tempstring = tempstring & vbCrLf & "rmdir " & Chr(34) & Directory.GetParent(path).ToString & Chr(34)
        tempstring = tempstring & vbCrLf & "REG DELETE HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp /f"
        tempstring = tempstring & vbCrLf & "del %0"
        My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.Temp & "\temp.bat", tempstring, False, System.Text.Encoding.Default)
        Shell(My.Computer.FileSystem.SpecialDirectories.Temp & "\temp.bat", AppWinStyle.Hide)
    End Sub
 

Sachith

Member
Joined
Jan 17, 2011
Messages
11
Location
Sri Lanka
Programming Experience
3-5
Go to project>>Ad Reference
@ the com tab you have to find the "Microsoft Scripting Runtime"
then click OK
after that you have to type


Dim fso As New Scripting.FileSystemObject
fso.DeleteFile("File Path", True)
 

d_a_r_k

Member
Joined
Oct 30, 2010
Messages
19
Programming Experience
Beginner
Thanks for your reply. I did everything you told. However I get an error saying that scrrun.dll does not exist though I checked it myself and it does exist.
 

Attachments

  • untitled.bmp
    407.2 KB · Views: 30

Sachith

Member
Joined
Jan 17, 2011
Messages
11
Location
Sri Lanka
Programming Experience
3-5
It Can't Be.. If You Find "Microsoft Scripting Runtime" in the Com Tab, That File Definitly Exists on (C:\Windows\System32\scrrun.dll)
Try Again!! Because that File is a Important File For Windows and It cat be Missing

or you Getting this msg again tell me I send you that File give me your E-mail Address
 

d_a_r_k

Member
Joined
Oct 30, 2010
Messages
19
Programming Experience
Beginner
Yeah you're right the code was a bit wrong. Anyway your method didn't work, the uninstaller doesn't even load anymore O_O. Good thing I came up with another code and now this case is solved.

VB.NET:
Imports System.IO

Module Module1

    Sub ProcessTree(ByVal Dir As String)
        Dim DirObj As New DirectoryInfo(Dir)
        Dim Files As FileInfo() = DirObj.GetFiles("*.*")
        Dim Dirs As DirectoryInfo() = DirObj.GetDirectories("*.*")
        Dim Filename As FileInfo
        Dim DirectoryName As DirectoryInfo
        For Each Filename In Files
            Try
                If (Filename.Attributes And FileAttributes.ReadOnly) Then
                    Filename.Attributes = (Filename.Attributes And Not FileAttributes.ReadOnly)
                End If
            Catch E As Exception
                Console.WriteLine("Error changing attribute for {0}", Filename.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try
        Next
        For Each DirectoryName In Dirs
            Try
                ProcessTree(DirectoryName.FullName)
            Catch E As Exception
                Console.WriteLine("Error accessing {0}", DirectoryName.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try
        Next
    End Sub

End Module
 
Top Bottom