0

I’m using this code to get a list of current proccess.

For Each Proc As Process In ProcessList
Dim ProcessList As List(Of Process) = Process.GetProcesses.ToList
 Dim Name As String = Proc.ProcessName
 Dim Path As String = Proc.MainModule.FileName
Dim Icon As System.Drawing.Image = System.Drawing.Icon.ExtractAssociatedIcon(Path).ToBitmap
next

but I get an error on Dim Path As String = Proc.MainModule.FileName) which I think is because I’m using 64bit OS.

Thanks in advance

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Sh3ewit
  • 3
  • 3
  • And the error you get is? Don't leave us guessing. – Flydog57 Oct 03 '21 at 02:22
  • By the way, you can simplify code like `Dim Name As String = Proc.ProcessName` to `Dim Name = Proc.ProcessName`. Since the `ProcessName` property is a string, your `Name` variable will be typed as a string. As long as the right hand side of an assignment is typed, you don't need to specify the type on a `Dim`/assignment statement – Flydog57 Oct 03 '21 at 02:26

1 Answers1

0

First of all you have ProcessList declared in your loop. I assume that was a paste error.

The problem is that you can't access all the processes like that. You will need to add a try catch.

Dim ProcessList As List(Of Process) = Process.GetProcesses.ToList
For Each Proc As Process In ProcessList
    Dim Name As String = Proc.ProcessName
    Dim v = Environment.Is64BitProcess
    Try
        Dim Path As String = Proc.MainModule.FileName
        Dim Icon As System.Drawing.Image = System.Drawing.Icon.ExtractAssociatedIcon(Path).ToBitmap
    Catch ex As Exception
        Debug.WriteLine("Can't Access File Name: " + ex.Message + ", Process Name: " + Name)
    End Try
Next

See here for more details.

How to avoid a Win32 exception when accessing Process.MainModule.FileName in C#?

Neil B
  • 2,096
  • 1
  • 12
  • 23