Hi there.

I am making an application in VB.Net. In which i am scanning some system folders and on the execution time it gives me the error;
E.g,

access to the path "C:\any system file" is denied.

Is there any code you people have that is edited in to the "form load" so when ever the application is executed, the denied error should be removed upon the form load?

Thanks..

the code is as follows

Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Security.Cryptography
Imports System.Text
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.CompilerServices
Imports System.Windows.Forms
Imports System.Collections.ObjectModel

Public Class Form1

    'Private Property strdir As String()

    'Private Property i As Integer

    Private Property QurantineFileName As String

    Private Property QuarantineInfoFileName As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button8.Enabled = False 'button at Scan Tab Clean
        Button9.Enabled = False 'button at Scan Tab Pause
        Button10.Enabled = False 'button at Scan Tab Resume
        Button5.Enabled = False 'button at Detected Tab Clean
        Button4.Enabled = False 'button at Detected Tab Delete
        Button3.Enabled = False 'button at Detected Tab Delete All
        Button11.Enabled = False 'button at Detected Tab Quarantine
        Button12.Enabled = False 'button at Detected Tab Quarantine All
        Button13.Enabled = False 'button at SafeZone Tab Clean
        Button14.Enabled = False 'button at SafeZone Tab Delete
        Button15.Enabled = False 'button at SafeZone Tab Delete All
        Button6.Enabled = False 'button at RT Protection Tab Enable
        Button16.Enabled = False 'button at Theme Tab Trizone


        

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        TabControl1.SelectTab(1)

        'Enabling Buttons

        Button8.Enabled = True
        Button9.Enabled = True
        Button10.Enabled = False
        Button5.Enabled = True
        Button4.Enabled = True
        Button3.Enabled = True
        Button11.Enabled = True
        Button12.Enabled = True


        Try
            For Each strDir As String In
            System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)

                For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                    ListBox1.Items.Add(strFile)

                Next

            Next
        Catch ex As Exception
        End Try

        Timer1.Start()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'The Code behind Full Scan Button.
        'FolderBrowserDialog1.SelectedPath = ("c:/")
        'System.Environment.GetFolderPath(Environment.SpecialFolder.)
        TabControl1.SelectTab(1)
        Dim path(14) As String
        path(1) = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
        path(2) = Environment.GetFolderPath(Environment.SpecialFolder.System)
        path(3) = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)
        path(4) = Environment.GetFolderPath(Environment.SpecialFolder.Recent)
        path(5) = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
        path(6) = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
        path(7) = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)
        path(8) = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
        path(9) = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        path(10) = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
        path(11) = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        path(12) = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
        path(13) = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        path(14) = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

        Dim i As Integer
        Dim value As Double

        For i = 1 To 14
            For Each File In My.Computer.FileSystem.GetFiles(path(i))
                value = value + 1
            Next
            For Each folder In My.Computer.FileSystem.GetDirectories(path(i))
                For Each file2 In My.Computer.FileSystem.GetFiles(folder)
                    value = value + 1
                Next
            Next
        Next

        


        Try
            For Each strDir As String In
            System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath(i))

                For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                    ListBox1.Items.Add(strFile)

                Next

            Next
        Catch ex As Exception
        End Try

        Timer1.Start()
    End Sub


    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Try
            labellastreal.Text = e.FullPath
            ListBox3.Items.Add(labellastreal.Text)
            Me.OpenFileDialog1.FileName = ""
            Dim scanbox As New TextBox
            scanbox.Text = My.Computer.FileSystem.ReadAllText("viruslist.txt").ToString
            Dim md5 As New MD5CryptoServiceProvider
            Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            md5.ComputeHash(f)
            Dim hash As Byte() = md5.Hash
            Dim buff As New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            If scanbox.Text.Contains(buff.ToString) Then
                Me.OpenFileDialog1.FileName = e.FullPath
                Form2.ShowDialog()

            End If

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            Dim ex As Exception = exception1
            ProjectData.ClearProjectError()
        End Try
    End Sub

    Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
        Try
            labellastreal.Text = e.FullPath
            ListBox3.Items.Add(labellastreal.Text)
            Me.OpenFileDialog1.FileName = ""
            Dim scanbox As New TextBox
            scanbox.Text = My.Computer.FileSystem.ReadAllText("viruslist.txt").ToString
            Dim md5 As New MD5CryptoServiceProvider
            Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            md5.ComputeHash(f)
            Dim hash As Byte() = md5.Hash
            Dim buff As New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            If scanbox.Text.Contains(buff.ToString) Then
                Me.OpenFileDialog1.FileName = e.FullPath
                Form2.ShowDialog()

            End If

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            Dim ex As Exception = exception1
            ProjectData.ClearProjectError()
        End Try
    End Sub

    Private Sub FileSystemWatcher1_Renamed(ByVal sender As System.Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
        Try
            labellastreal.Text = e.FullPath
            ListBox3.Items.Add(labellastreal.Text)
            Me.OpenFileDialog1.FileName = ""
            Dim scanbox As New TextBox
            scanbox.Text = My.Computer.FileSystem.ReadAllText("viruslist.txt").ToString
            Dim md5 As New MD5CryptoServiceProvider
            Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            md5.ComputeHash(f)
            Dim hash As Byte() = md5.Hash
            Dim buff As New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            If scanbox.Text.Contains(buff.ToString) Then
                Me.OpenFileDialog1.FileName = e.FullPath
                Form2.ShowDialog()

            End If

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            Dim ex As Exception = exception1
            ProjectData.ClearProjectError()
        End Try
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        FileSystemWatcher1.EnableRaisingEvents = True
        realtime.Text = "Real-Time Enabled"
        Button6.Enabled = False
        Button7.Enabled = True





    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        FileSystemWatcher1.EnableRaisingEvents = False
        realtime.Text = "Real-Time Disabled"
        Button6.Enabled = True
        Button7.Enabled = False



    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            Kill(ListBox2.SelectedItem)
            ListBox2.Items.Remove(ListBox2.SelectedItem)



            MsgBox("Threat Was Removed Successfully!", MsgBoxStyle.Information)


        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Timer2.Start()
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Try
            If Not ListBox2.Items.Count = 0 Then
                ListBox2.SelectedIndex += 1
                Kill(ListBox1.SelectedItem)
                ListBox2.Items.Remove(ListBox2.SelectedItem)
            Else
                Timer1.Stop()
                Timer2.Stop()

                MsgBox("Threat Was Removed Successfully!", MsgBoxStyle.Information)

            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Maximum = Conversions.ToString(ListBox1.Items.Count)
        total.Text = Conversions.ToString(ListBox1.Items.Count)

        If Not ProgressBar1.Value = ProgressBar1.Maximum Then
            Try

                ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
                TextBox1.Text = ListBox1.SelectedItem.ToString
            Catch ex As Exception
            End Try



            Try

                Dim scanbox As New TextBox
                Dim read As String = My.Computer.FileSystem.ReadAllText("viruslist.txt")
                ProgressBar1.Increment(1)
                detected.Text = Conversions.ToString(ListBox2.Items.Count)
                files.Text = Conversions.ToString(ProgressBar1.Value)
                scanbox.Text = read.ToString
                Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
                Dim f As FileStream = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                f = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                md5.ComputeHash(f)
                Dim hash As Byte() = md5.Hash
                Dim buff As StringBuilder = New StringBuilder
                Dim hashByte As Byte
                For Each hashByte In hash
                    buff.Append(String.Format("{0:X2}", hashByte))
                Next

                If scanbox.Text.Contains(buff.ToString) Then



                    ListBox2.Items.Add(ListBox1.SelectedItem)
                End If
            Catch ex As Exception
            End Try
        Else
            Timer1.Stop()
            MsgBox("Finished Scanning Folder!")
            TabControl1.SelectTab(2)
            If ListBox1.Items.Count = 0 Then
                MsgBox("No Threats were detected, Scan Window will close!", MsgBoxStyle.Information)

            End If
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Timer1.Stop()
        If detected.Text = "0" Then
            'Form3.Show()
            MsgBox("Are you sure u want to stop the scan process ?", MsgBoxStyle.YesNo)

            'If MsgBoxResult.Yes Then
            'TabControl1.SelectTab(0)
            'files.Text = "0"
            'total.Text = "0"
            'detected.Text = "0"
            'ProgressBar1.Value = 0
            'TextBox1.Clear()
            'Timer1.Stop()
            'ElseIf MsgBoxResult.No Then
            'Timer1.Start()
            If DialogResult.No Then
                Timer1.Start()

            ElseIf DialogResult.Yes Then
                TabControl1.SelectTab(0)
                files.Text = "0"
                total.Text = "0"
                detected.Text = "0"
                ProgressBar1.Value = 0
                TextBox1.Clear()
                Timer1.Stop()

            ElseIf detected.Text <> "0" Then
                MsgBox("The preliminary scan detects some infection(s). Do you want to heal these infection(s) ?", MsgBoxStyle.YesNoCancel)
                If DialogResult.Cancel Then
                    Timer1.Start()
                ElseIf DialogResult.No Then
                    TabControl1.SelectTab(0)
                    Timer1.Stop()
                Else
                    TabControl1.SelectTab(2)
                End If
                End If

            End If

    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        'Buttons on Safe zone tab enabled
        Button13.Enabled = True
        Button14.Enabled = True
        Button15.Enabled = True
        TabControl1.SelectTab(3)

        Dim QurantineeFileName As String = System.Guid.NewGuid().ToString("")
        Dim QuarantineeInfoFileName = QuarantineeFileName() & ".log"
        
        Dim QuarantineFolder As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("f:/Quarantine")
        'If QuarantineFolder.Exists = False Then
        'Try
        'QuarantineFolder.Create()
        'Catch ex As Exception
        'MsgBox("Error creating the quarantine directory: " & ex.Message)
        '
        '   Maybe you want to wait for a Cancel or Retry
        '
        'End Try
        'End If
        Dim OriginalFile As System.IO.FileInfo = New System.IO.FileInfo(TextBox1.Text)
        Dim DestinationFile As System.IO.FileInfo = New System.IO.FileInfo(QurantineeFileName)
        Dim DestinationInfoFile As System.IO.FileInfo = New System.IO.FileInfo(QuarantineeInfoFileName)
        '
        '
        '
        'Do While DestinationFile.Exists Or DestinationInfoFile.Exists
        'QurantineeFileName = System.Guid.NewGuid().ToString()
        'QuarantineeInfoFileName = Quarantinefilename() & ".log"
        'DestinationFile = New System.IO.FileInfo(QurantineeFileName)
        'DestinationInfoFile = New System.IO.FileInfo(QuarantineeInfoFileName)
        'Loop

    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Button11.Enabled = False ' quarantine Button Disabled
        'Buttons on Safe zone tab enabled
        Button13.Enabled = True
        Button14.Enabled = True
        Button15.Enabled = True
        TabControl1.SelectTab(3)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Timer1.Stop()
        Button9.Enabled = False
        Button10.Enabled = True
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Timer1.Start()
        Button10.Enabled = False
        Button9.Enabled = True
    End Sub

    Private Function QuarantineeFileName() As String
        Throw New NotImplementedException
    End Function

    Private Function QuarantineFolder() As Object
        Throw New NotImplementedException
    End Function

    Private Function Quarantinefilename() As String
        Throw New NotImplementedException
    End Function



End Class

As far as I know you can't override that behaviour from within the application. Access to system folders is restricted to protect your system. If it could be overridden from with the application with some code then the system would not be aable to protect itself from malicious code. There are ways to run an application with elevated priviledges. One way is to select "Run As Administrator" from the shortcut context menu but you have to do that every time you run it AND you get a confirmation prompt every time. The other way avoids the prompt but requires creating an entry in the Task Scheduler.

Jim is right, but even as administrator you can't access some pretty hiden folders as C:\System Volume Information
You need your app to work with SYSTEM priviledges, but I don't think my AV runs under these, so perhaps there is another way (some system function perhaps?)

YA. that is the main issue i am facing when scannning some of system folders. I don't know how to remove these securities. I tried to minimize UAC from control panel but that is not working and not the solution....

This might be just rubish, but what if you created a service running under system?
Again I never tried this, but I don't see a reason why this wouldn't work.

What the hell is this, in order to be a good programmer, we must know a lot about windows,,,, shit man....
now I am stuck in the middle of my project due to these securities..lol

You can be a good programmer without knowing windows. Unfortunatelly you'll have to know Unix/Linux/MacOS/whatever. Since you are interacting with the OS you have to know stuff about it.

Best solution would be to edit the App Manifest on Application TAb of the project properties. Set the requestedExececutionLevel to requireAdministrator

Didn't we discuss that admin rights won't cut it?

Hello. I am working on an anti-virus in VB.Net. I have been having the problem that when i run a full scan, it gives me an error saying "Access to the path "C:\Documents and Settings" is denied.". Please help. If you need my code just ask.

your file is readonly. do

System.IO.File.SetAttributes(path, IO.FileAttributes.Normal)

have you tried turning UAC (User Access Control) off?
Start > Search > UAC

Member Avatar for deletedaccount

The code provided above is a basic Anti-Virus made in VB.NET. He is trying to scan through the MD5 of applications and compare them to a database file. This is correct to making a Anti-Virus, but is MD5 the best option? Yes, and no.

Check the Windows API. May be something in there for accessing permission.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.