Results 1 to 17 of 17

Thread: [RESOLVED]proc.MainModule.FileName has repeated file path.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    Question [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:
    1. Private Sub frmmain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.         Dim Process As New Process()
    3.         Dim Count As Integer = 0
    4.         lstvproc.Items.Clear()
    5.  
    6.         For Each Process In Process.GetProcesses(My.Computer.Name)
    7.             On Error Resume Next
    8.             lstvproc.Items.Add(Process.ProcessName() + ".exe")
    9.             lstvproc.Items(Count).SubItems.Add(FormatNumber(Process.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
    10.             If Process.MainModule.FileName = "" Then    <<< starts here.
    11.                 lstvproc.Items(Count).SubItems.Add("")
    12.             Else
    13.                 lstvproc.Items(Count).SubItems.Add(Process.MainModule.FileName)
    14.             End If
    15.             lstvproc.Items(Count).SubItems.Add(Process.Id)
    16.             Count += 1
    17.  
    18.         Next
    19.         tmrcpu.Start()
    20.         tlblproc.Text = "Processes: " & lstvproc.Items.Count
    21.     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.

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Process1.MainModule.FileName has repeated path.

    Quote Originally Posted by Refuse View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    Re: Process1.MainModule.FileName has repeated path.

    Ill show you some pictures.


    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.



    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.
    Last edited by Refuse; Dec 26th, 2009 at 12:16 AM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Try
    2.     'Try to add a subitem containing the file name.
    3. Catch
    4.     'Add a blank subitem.
    5. End Try
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    Re: Process1.MainModule.FileName has repeated path.

    So it would be something like this?:
    vb Code:
    1. For Each Process In Diagnostics.Process.GetProcesses(My.Computer.Name)
    2.             Try
    3.                 lstvproc.Items.Add(Process.ProcessName() + ".exe")
    4.                 lstvproc.Items(count).SubItems.Add(FormatNumber(Process.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
    5.                 lstvproc.Items(count).SubItems.Add(Process.MainModule.FileName) 'Here i try...
    6.                 lstvproc.Items(count).SubItems.Add(Process.Id)
    7.                 count += 1
    8.             Catch
    9.                 lstvproc.Items(count).SubItems.Add("Access Denied") 'Doesnt add a blank but adresses the problem.
    10.             End Try
    11.         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?

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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:
    1. Dim count As Integer = 0
    2.         lstvproc.Items.Clear()
    3.  
    4.         For Each proc In Diagnostics.Process.GetProcesses(My.Computer.Name)
    5.             Try
    6.                 lstvproc.Items.Add(proc.ProcessName() + ".exe")
    7.                 lstvproc.Items(count).SubItems.Add(FormatNumber(proc.WorkingSet64 / 1024, NumDigitsAfterDecimal:=0) & " K")
    8.                 lstvproc.Items(count).SubItems.Add(proc.MainModule.FileName)
    9.                 lstvproc.Items(count).SubItems.Add(proc.Id)
    10.                 count += 1
    11.             Catch
    12.                 lstvproc.Items(count).SubItems.Add("Access Denied")
    13.                 lstvproc.Items(count).SubItems.Add(proc.Id)
    14.                 count += 1
    15.             End Try
    16.         Next
    17.  
    18.         tmrcpu.Start()
    19.         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.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    30

    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

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width