Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 12-Sep-17 21:02pm
Comments
Richard MacCutchan 13-Sep-17 2:55am    
We cannot guess what errors you get, or when you get them.

1 solution

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.
 
Share this answer
 
Comments
jumper77 13-Sep-17 11:26am    
Thank you for the reply. But I think I may not have explained myself well. About the exception, it is always the same. Access Denied. That's the only exception. And if it's going to happen, it always occurs on the second line of the "Try" block.

I have no problem writing the files. The files are only written to show the results of trying to get the path to the exe in question.

I'm just trying to find out "why" I'm getting the access denied exception. I can use the try catch block to decide whether to add the item to the Listview.
OriginalGriff 13-Sep-17 11:36am    
You mean it happens on this line:

Dim exePath As String = p.Modules(0).FileName

Odd.
Start by breaking that line out

Dim x = p.Modules
Dim y = x(0)
Dim z = y.FileName
Dim exePath As String = z

And see if that helps you identify which part is wrong.
jumper77 13-Sep-17 12:09pm    
Thank you for the reply. And yes, the exception occurs on the line you said (p.Modules(0).Filename). I tried your code suggestion and I get get the exception on "Dim x = p.Modules", if there is going to be an exception.

I apologize for my lack of understanding about what's going on with the code. Right now I can't think of anything else to try. I hope you might though :)
OriginalGriff 13-Sep-17 12:26pm    
If you look at the documentation for the Modules property:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.modules(v=vs.110).aspx
it does say

Win98WinMe
This property is not available on this platform if you started the process with ProcessStartInfo.UseShellExecute set to true.

I don't know if it also applies to later OSes (MS can be a little flaky about updating documentation). But I'd imagine you could also get that trying to access (say) a 64 bit process from a 32 bit app, or possibly accessing a system process from a user app.
Have you checked which process(es) are throwing the exception?

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