【问题标题】:getting file path in vb.net在 vb.net 中获取文件路径
【发布时间】:2021-10-03 01:38:41
【问题描述】:

我正在使用此代码获取当前进程的列表。

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

但我在Dim Path As String = Proc.MainModule.FileName) 上收到一个错误,我认为这是因为我使用的是 64 位操作系统。

提前致谢

【问题讨论】:

  • 你得到的错误是?不要让我们猜测。
  • 顺便说一句,您可以将Dim Name As String = Proc.ProcessName 之类的代码简化为Dim Name = Proc.ProcessName。由于ProcessName 属性是一个字符串,您的Name 变量将被键入为一个字符串。只要键入了赋值的右侧,就不需要在Dim/assignment 语句中指定类型

标签: .net vb.net


【解决方案1】:

首先,您在循环中声明了 ProcessList。我认为这是一个粘贴错误。

问题是您不能像那样访问所有进程。您需要添加一个 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

查看这里了解更多详情。

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

【讨论】:

    猜你喜欢
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2013-07-17
    相关资源
    最近更新 更多