获取访问被拒绝异常试图获得应用程序的完整路径 [英] Getting access denied exception trying to get full path to application

查看:58
本文介绍了获取访问被拒绝异常试图获得应用程序的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Listview,其中包含具有TCP连接的应用程序列表。我希望能够提取应用程序图标以将其添加到Listview列。但要做到这一点,我需要exe的完整路径。



问题是我在某些程序上遇到异常,但在其他程序上却没有。我获得了进程Idle和svchost的异常。这对我来说很有意义。我可以在try catch块中捕获它并使用通用应用程序图标。



但我也得到了我自己安装的程序的例外情况。我真的不明白的一个奇怪的事情是我安装的应用程序之一有几个TCP连接。有些我可以通往,有些我不能。



我希望有人可以告诉我发生了什么。我很感激。

这是我正在使用的代码:



我尝试了什么:



 processName = GetProcessName(Info.PID)
尝试
Dim p As Process = Process.GetProcessById(Info.PID)
Dim exePath As String = p.Modules(0).FileName
使用writer As StreamWriter = New StreamWriter(Ok.txt,True)
writer.WriteLine(processName&path is:& ; exePath)
结束使用

Catch ex As Exception
使用writer As StreamWriter = New StreamWriter(error.txt,True)
writer.WriteLine(无法获取进程的路径:& processName)
结束使用
结束尝试

解决方案

< blockquote>问题是你丢弃了你需要的信息:异常细节。你要做的只是抓住它,忽略它,并提出你自己的通用消息,其中不包含有关它发生原因的信息。使用调试器,在Try块的第一行放置一个断点,然后单步执行代码。当它超越时,使用Exception对象来收集更多信息。



但是你说它是访问被拒绝意味着问题是:它就是你写的地方文件。因为您没有指定路径,所以它位于当前文件夹中:应用程序文件夹 - 用于发布应用程序的文件夹位于Program Files下,并且为了安全起见,写入保护。这种代码在开发中工作,因为文件夹结构不同:调试应用程序从bin / debug文件夹运行,并从bin / release释放,两者都没有任何写入限制。但是在发布时?它失败了。



看看这里:我应该在哪里存储我的数据? [ ^ ] - 代码在C#中,而不是VB,但很明显。


I have a Listview that contains a list of applications that have TCP connections. I want to be able to extract the application icon to add it to the Listview column. But to do that, I need the full path to the exe.

The problem is I'm getting an exception on some programs, but not on others. I get exceptions for processes Idle and svchost. That make sense to me. I could trap that in a try catch block and use a generic app icon.

But I'm also getting exceptions for programs that I have installed myself. One weird thing I really don't understand is one of the apps I installed has several TCP connections. Some I can get the path to, some I can't.

I hope someone can tell me what's going on. I would appreciate that.
Here's the code I'm using:

What I have tried:

processName = GetProcessName(Info.PID)
Try
     Dim p As Process = Process.GetProcessById(Info.PID)
     Dim exePath As String = p.Modules(0).FileName
     Using writer As StreamWriter = New StreamWriter("Ok.txt", True)
          writer.WriteLine(processName & "path is: " & exePath)
     End Using

Catch ex As Exception
     Using writer As StreamWriter = New StreamWriter("error.txt", True)
          writer.WriteLine("Could not get path for process: " & processName)
     End Using
End Try

解决方案

The problem is that you are throwing away the information you need: the exception detail. All you do is catch it, ignore it, and put up your own generic message which contains no information on why it happened. Use the debugger, put a breakpoint on the first line of the Try block, and step your code. When it excepts, use the Exception object to gather more info.

But that you say it's an "access denied" implied what the problem is: it's where you are writing the file. Because you don't specify a path, it goes in the current folder: the application folder - which for release apps will be under "Program Files" and those write protected for safety. This kind of code works in development because the folder structure is different: debug apps run from the bin/debug folder and release from the bin/release, neither of which have any write restrictions. But on release? It fails.

Have a look here: Where should I store my data?[^] - the code is in C#, not VB but it's pretty obvious.


这篇关于获取访问被拒绝异常试图获得应用程序的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆