[RESOLVED]proc.MainModule.FileName has repeated file path.
As in the title, in my program, i use that code for getting the file path of a process, once it gets the path, it is put into a list view which has details like the task manager. the code works fine the first time, but with a little bug that is, when it reaches the vhost of my program. For some reason, it just copies the file path of my program even though the process has changed. After the code has gone through, i want it to refresh the list so as doing the same as when loaded...so I let it refresh and once again it copies the path of the vhost, but this time it has basically taken over every process' location. So in turn, I only get returned my programs location, for something like the task managers location, where it would be like "C:\Windows\system32\tskmgr.exe"
Here's the code:
VB Code:
Private Sub frmmain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Process As New Process()
Dim Count As Integer = 0
lstvproc.Items.Clear()
For Each Process In Process.GetProcesses(My.Computer.Name)
On Error Resume Next
lstvproc.Items.Add(Process.ProcessName() + ".exe")
lstvproc.Items(Count).SubItems.Add(FormatNumber(Process.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
If Process.MainModule.FileName = "" Then <<< starts here.
lstvproc.Items(Count).SubItems.Add("")
Else
lstvproc.Items(Count).SubItems.Add(Process.MainModule.FileName)
End If
lstvproc.Items(Count).SubItems.Add(Process.Id)
Count += 1
Next
tmrcpu.Start()
tlblproc.Text = "Processes: " & lstvproc.Items.Count
End Sub
The refresh code is frmmain_Load(Nothing, Nothing) just to make things easy for me right now, while i get that part of the program going.
To me it seems to be working fine, but why it does that what it does, im not sure. as you can see, the process changes. and Ive even checked it through run time, and each and every time, the process.processname changes. Any help will be greatly appreciated, thanks in advance.
Re: Process1.MainModule.FileName has repeated path.
I'm not sure I know what you're asking exactly.
The vshost file path is correct. If you look into your debug folder, you'll find the vshost there.
Re: Process1.MainModule.FileName has repeated path.
well yes, the vhosts path is correct, but lets say the process is tskmgr.exe and i wanted to get its path, it goes through the code the first time and it returns with its exact place, but once i refresh, it says the path to tskmgr.exe is the vhosts path...which is not correct.
Re: Process1.MainModule.FileName has repeated path.
First up, get rid of this line:
Code:
Dim Process As New Process()
That is not doing anything useful and it's actually creating a Process object that you never use, so it's inefficient.
Secondly, it's really a bad idea to use a type name as the name of a variable of that type. It simply make your code confusing. Also, you should be consistent and name ALL your variables starting with a lower-case letter. As such, your 'Process' variable should become 'proc' or 'currentProcess' or something like that.
Thirdly, you really should get rid of that "On Error Resume Next" and NEVER, EVER use it again. If there's an error, why would you want to simply ignore it and continue? If there's an error, don't you want to know what it is so that you can fix it? If there are legitimate reasons that an exception may be thrown in your code then you should use structured exception handling, i.e. a Try...Catch block, and examine the exception to see what you need to do to fix it.
Finally, I can't see what that If block is achieving anyway. You say that if the file name is an empty string then add an empty string and if it's not then add the file name. If the file name is an empty string and you add the file name, aren't you adding an empty string anyway? If so, what's the If block for?
I suggest that you address all those points and then post back if you still have an issue.
Re: Process1.MainModule.FileName has repeated path.
I will do as you have requested, thank you for the advice jmcilhinney, and yes your right i would want to fix the error. Now the reason the If statement is in there is because when i run the program, (without the if statement) the statement "lstvproc.Items(Count).SubItems.Add(Process.Id)" of that process that has an empty path, actually moves to the column of where the location is, so the location displays as the "id" of the process, which i dont want, so i fill in the blank and put "". and it works. though, now i guess the reason it does that it would be because of the error?
Re: Process1.MainModule.FileName has repeated path.
Quote:
Originally Posted by
Refuse
Now the reason the If statement is in there is because when i run the program, (without the if statement) the statement "lstvproc.Items(Count).SubItems.Add(Process.Id)" of that process that has an empty path, actually moves to the column of where the location is
Well, no, that would only happen if you did this:
Code:
If Process.MainModule.FileName <> "" Then
lstvproc.Items(Count).SubItems.Add(Process.MainModule.FileName)
End If
but if you just did this:
Code:
lstvproc.Items(Count).SubItems.Add(Process.MainModule.FileName)
then you're covered because, if the file name is an empty string, you're going to add an empty string.
Re: Process1.MainModule.FileName has repeated path.
Ill show you some pictures.
http://i91.photobucket.com/albums/k3.../Without-1.jpg
in this picture, without the if statement, its all out of whack, i even tried it with the old code and without the if statement, and it came out the same way.
http://i91.photobucket.com/albums/k3.../VB/With-1.jpg
In this picture, i use the old cold and the if statement, and its the way it should be. just with still the same problem with the process.mainmodule.filename.
Re: Process1.MainModule.FileName has repeated path.
There's more at work there than you think. If you look closely you'll see that it hasn't just missed a file name. Part of the problem is that you're ignoring exceptions again. You've got a Try...Catch block in there but you aren't doing anything in the Catch block. I think you'll find that an exception is being thrown but you are simply ignoring it and carrying on. You need to actually examine the exception to see what the problem is.
Re: Process1.MainModule.FileName has repeated path.
Yes, im actually doing that right now. ive never used a try..catch block before so i had to look it up just now. it actually returns with about i think 7 or 8 exceptions (with the try..catch code, and without the if statement), each leading me to line 14, mostly saying access denied. Now if i could only understand what it was trying to tell me, haha.
Re: Process1.MainModule.FileName has repeated path.
I got the code like this :
Code:
Try
lstvproc.Items.Add(Process.ProcessName() + ".exe")
lstvproc.Items(count).SubItems.Add(FormatNumber(Process.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
lstvproc.Items(count).SubItems.Add(Process.MainModule.FileName)
lstvproc.Items(count).SubItems.Add(Process.Id)
count += 1
Catch ex As Exception
MsgBox(ex.ToString)
End Try
but, im not sure if its the right way to figure out what i need to do? Using that catch statement i mean.
Re: Process1.MainModule.FileName has repeated path.
I can't see all your code but it doesn't make sense to me that accessing the MainModule in one place throws an exception and in another it doesn't. I just tried this code:
Code:
For Each proc In Process.GetProcesses
Try
MessageBox.Show(proc.MainModule.FileName)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Next
and got a bunch of exceptions. I then tried this code:
Code:
For Each proc In Process.GetProcesses
Try
If proc.MainModule.FileName = "" Then
MessageBox.Show("Cannot access file name.")
Else
MessageBox.Show(proc.MainModule.FileName)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Next
and got exactly the same exceptions.
Re: Process1.MainModule.FileName has repeated path.
I see what you are saying with those codes, that either way even if it was an if statement or not, it would still show exceptions. i might have misinterpreted but that's what i got out of it. If you want to see all the code then i would have no problem in showing you, unless you meant he whole code meaning the one i just posted. Now to fix the exception? the only thing i can do is if the access is denied, i would have to fill in the blank, with "Access Denied". unless your trying to make me think of another way of actually clearing this problem.
Re: Process1.MainModule.FileName has repeated path.
Generally speaking, you should avoid using exception handling to control the flow of execution of your code. It's almost always better to pre-validate and avoid an exception that to "suck it and see". In this case though, there may not be a way to check beforehand whether the FileName will be accessible. If not then you should really do this:
vb.net Code:
Try
'Try to add a subitem containing the file name.
Catch
'Add a blank subitem.
End Try
Re: Process1.MainModule.FileName has repeated path.
So it would be something like this?:
vb Code:
For Each Process In Diagnostics.Process.GetProcesses(My.Computer.Name)
Try
lstvproc.Items.Add(Process.ProcessName() + ".exe")
lstvproc.Items(count).SubItems.Add(FormatNumber(Process.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
lstvproc.Items(count).SubItems.Add(Process.MainModule.FileName) 'Here i try...
lstvproc.Items(count).SubItems.Add(Process.Id)
count += 1
Catch
lstvproc.Items(count).SubItems.Add("Access Denied") 'Doesnt add a blank but adresses the problem.
End Try
Next
Thats what i got out of it. Though, it does add access denied to the places where it needs to, but when it hits one, it still gives fake paths, to most of the rest of them in the rest of the list, and some are not in the right spot, so now im stuck. one thing, with a try...catch block, does it run the try method, but when it hits an error, does it go out to the catch, does the command from catch, and then go back into the try and continues from where it left off? or does it just start over again the try?
Re: Process1.MainModule.FileName has repeated path.
Well i fixed that little problem i had last night, i guess the try..catch block does go out of focus and then goes back to the beginning of the try. So i just added what had to be done after. :
vb Code:
Dim count As Integer = 0
lstvproc.Items.Clear()
For Each proc In Diagnostics.Process.GetProcesses(My.Computer.Name)
Try
lstvproc.Items.Add(proc.ProcessName() + ".exe")
lstvproc.Items(count).SubItems.Add(FormatNumber(proc.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
lstvproc.Items(count).SubItems.Add(proc.MainModule.FileName)
lstvproc.Items(count).SubItems.Add(proc.Id)
count += 1
Catch
lstvproc.Items(count).SubItems.Add("Access Denied")
lstvproc.Items(count).SubItems.Add(proc.Id)
count += 1
End Try
Next
tmrcpu.Start()
tlblproc.Text = "Processes: " & lstvproc.Items.Count
Im still stuck though, because the proc.mainmodule.filename still gives me a path of another process, which i still dont understand why it does that.
Re: proc.MainModule.FileName has repeated file path.
Ah, i had found the problem, for some reason when it is run from the debugger it gives me repeated paths, but now if i ran it just alone, it gave me the right information, why it does this, i dont know. But in this case, this is resolved. and thanks for the much appreciated help jmcilhinney
Re: [RESOLVED]proc.MainModule.FileName has repeated file path.
@jmc
This, "suck it and see", struck me as extremely funny. I have never heard that one. Thanks.