Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This question is asking for improvements to the code I wrote to remove MS & Chrome files that keep being downloaded to my computer. In this case it is W7 64 bit with VS 2019 and I only use Firefox.
I have VS set to no updates and the only MS update is MS Security Essentials which is set to
Check for Updates & Let me choose to Download & Install"
Every time I start my computer MS sends this message and If I let the computer idle for
10 min I get 5 of these messages.

Warning: Failed to get value SOFTWARE\Microsoft\VisualStudio\16.11\ExtensionManager/ExtensionsAutoUpdated: Object reference not set to an instance of an object.
[13d0:0001][2024-08-19T10:13:50] No update since last run. Skipping current operation.


Please I am a novice and even after using the App for a few days I wonder if it is has any value. I did learn how System IO works so the time spent was worth it IMHO.

What I have tried:

VB.NET
Imports System.IO
Imports System.Security.Principal

Public Class frmStart
    Public Sub frmStart_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = 27 Then
            Application.Exit()
        End If
    End Sub

    Private Sub btnVCache_Click(sender As Object, e As EventArgs) Handles btnVCache.Click

        lstFiles.Items.Clear()
        Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data\"
        Process.Start(folderPath)

    End Sub

    Private Sub btnVTemp_Click(sender As Object, e As EventArgs) Handles btnVTemp.Click

        lstFiles.Items.Clear()
        Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Temp\"
        Process.Start(folderPath)

    End Sub

    Private Sub btnDelCache_Click(sender As Object, e As EventArgs) Handles btnDelCache.Click

        Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data\"
        Dim filePattern As String = "f*"
        Dim files As IEnumerable(Of String) = Directory.EnumerateFiles(folderPath, filePattern)

        lstFiles.Items.Clear()
        Try

            If files.Any() Then
                gvType = "6"
                frmAlert.ShowDialog()
                gvType = "7"
                frmAlert.ShowDialog()

                If gvResult = "YES" Then

                    For Each fileName As String In Directory.GetFiles(folderPath)
                        Dim dirInfo As New DirectoryInfo(fileName)
                        Dim fC As String = fileName.Replace(folderPath, String.Empty)

                        If fC.Substring(0, 1) = "f" Then
                            lstFiles.Items.Add(fileName.Replace(folderPath, String.Empty))
                            My.Computer.FileSystem.DeleteFile(fileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
                        End If
                    Next

                    gvType = "3"
                    frmAlert.ShowDialog()

                ElseIf gvResult = "NO" Then 'result = MsgBoxResult.No Then

                    Dim file As String = folderPath.Replace(folderPath, String.Empty)
                    For Each file In files
                        Dim fileName As String = Path.GetFileName(file)
                        lstFiles.Items.Add(fileName)
                    Next

                End If

            Else
                gvType = "5"
                frmAlert.ShowDialog()
            End If

        Catch ex As UnauthorizedAccessException
            MessageBox.Show("Access to the folder is denied.")
        Catch ex As DirectoryNotFoundException
            MessageBox.Show("The specified folder does not exist.")
        Catch ex As Exception
            MessageBox.Show("An error occurred: " & ex.Message)
        End Try

    End Sub

    Private Sub btnDelTemp_Click(sender As Object, e As EventArgs) Handles btnDelTemp.Click

        Dim folderPath As String = "C:\Users\Dwight\AppData\Local\Temp\"
        Dim files As String() = Directory.GetFiles(folderPath, "dd*")

        lstFiles.Items.Clear()

        ' Check if any files are found
        If files.Length = 0 Then
            gvType = "1"
            frmAlert.ShowDialog()

            Exit Sub
        End If

        gvType = "2"
        frmAlert.ShowDialog()

        If gvResult = "YES" Then
            For Each file As String In files
                lstFiles.Items.Add(file.Replace(folderPath, String.Empty))
                ' Send file to recycle bin
                My.Computer.FileSystem.DeleteFile(file, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
            Next

            gvType = "3"
            frmAlert.ShowDialog()

        ElseIf gvResult = "NO" Then
            gvType = "4"
            frmAlert.ShowDialog()
            Exit Sub
        End If

    End Sub

    Private Sub btnOpenRB_Click(sender As Object, e As EventArgs) Handles btnOpenRB.Click

        lstFiles.Items.Clear()

        Dim userSid As String = WindowsIdentity.GetCurrent().User.Value

        ' Construct the path to the Recycle Bin for the current user
        Dim recycleBinPath As String = String.Format("C:\$Recycle.Bin\{0}", userSid)

        ' Start a new process to open Windows Explorer at the Recycle Bin path
        Process.Start("explorer.exe", recycleBinPath)

    End Sub

End Class

frmAlert
VB.NET
Public Class frmAlert

    Private Sub btnYes_Click(sender As Object, e As EventArgs) Handles btnYes.Click
        gvResult = "YES"
        Close()
    End Sub

    Private Sub btnNo_Click(sender As Object, e As EventArgs) Handles btnNo.Click
        gvResult = "NO"
        Close()
    End Sub

    Public Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Close()
    End Sub
    Public Sub SetOKTrue()
        btnYes.Visible = False
        btnNo.Visible = False
        btnOK.Visible = True
    End Sub
    Public Sub SetOKFalse()
        btnYes.Visible = True
        btnNo.Visible = True
        btnOK.Visible = False
    End Sub
    Private Sub frmAlert_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If gvType = "1" Then
            tbAlert.Text = vbCrLf & "No Files to Delete"
            SetOKTrue()
        ElseIf gvType = "2" Then
            tbAlert.Text = vbCr & "YES to Delete Files" & vbCrLf & vbCrLf & "NO to EXIT            "
            SetOKFalse()
        ElseIf gvType = "3" Then
            tbAlert.Text = vbCrLf & "Files Deleted"
            SetOKTrue()
        ElseIf gvType = "4" Then
            tbAlert.Text = vbCrLf & "NO Files DELETED"
            SetOKTrue()
        ElseIf gvType = "5" Then
            tbAlert.Text = vbCrLf & "NO Files With" & "   f   " & "FOUND"
            SetOKTrue()
        ElseIf gvType = "6" Then
            tbAlert.Text = vbCrLf & "Files FOUND"
            SetOKTrue()
        ElseIf gvType = "7" Then
            tbAlert.Text = vbCr & "Yes to Delete Files" & vbCrLf & vbCrLf & "NO ONLY View Files"
            SetOKFalse()


        End If
    End Sub

End Class

Module
VB
Module AlertModule

    Public gvType As String
    Public gvResult As String

End Module
Posted

Without knowing where in your code the error is being thrown, there isn't really anything we can do. Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!

And though you can run VS2019 on Win7, it's not a good idea. Support for Win7 ended on Jan 14th 2020, and no new security updates are produced - which means that your computer (and thus the whole network it is connected to) are vulnerable to malware, viruses, ransomware, and a host of other nasties.

I would seriously consider updating your OS, you really shouldn't have any physical access to the internet until you do!
 
Share this answer
 
Comments
Choroid 20-Aug-24 11:04am    
Griff I have never ventured to using the debugger till the other day. It was not good for one reason I am trying to debug with a app that is looking at files that are in use or open in this case VS. Any way it locked up the system. Need to figure out how to debug with a installed app if that is possible. More searching and reading. Also need to look at Pete's ExtensionManager suggestion. BIG Thank You
OriginalGriff 21-Aug-24 3:09am    
Learn to use the debugger, it really is your best friend in development!
There are very rare cases when you can't use a debugger - mostly related to a problem a remote user has that you can't duplicate locally - and then you fall back on old-school methods: logging what the app is doing and to what. It's slow, very slow: add code to write to a log file then when the problems happens examine the log and add more focussed code to add better info to the log file. Repeat until the problem becomes clearer. I grew up on that, because debuggers weren't a thing back then, and knowing how to use a debugger makes it so, so much simpler and quicker. Give it a proper try and see what info you can get.
Griff has summarised, perfectly, what is happening here. The error you are seeing here relates to something reading from the registry, and there's nowhere in your code that you show that you are doing that. What you need to be looking for, and you do have the information to do this already, is search for ExtensionManager in your code. That string should be enough of a clue for you to be able to find the relevant spot, and fix it.
 
Share this answer
 
Comments
Choroid 20-Aug-24 11:08am    
Pete YES as a Novice I never learned to use the debugger in VS. I tried the other day and it did not go well. I was trying to debug where the files were open or in use big NO NO. Will look into the ExtensionManager Thanks for taking the time to respond it is very appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900