Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I get the filename and sourcepath of a running process?

How can I get the filename and sourcepath of a running process?

Scheduled Pinned Locked Moved C#
questionhelptutorial
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Erik

    Hi, I am using something like this to get all my running processes:

          Process\[\] processlist = Process.GetProcesses();
          foreach(Process theprocess in processlist)
          {
                 Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
          }
    

    My problem is that I want to know the filename (EXE) of a process, and the pathname it was started from. For example, if there is a process Notepad.exe running, I want to determine in which folder that file actually resides, i.e. something like "C:\Windows\system32\notepad.exe". How can I get that information? I already tried to get the member StartInfo. but it doesn't contain a link to the folder. How can I do this?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    theprocess.MainModule.FileName

    R 1 Reply Last reply
    0
    • E Erik

      Hi, I am using something like this to get all my running processes:

            Process\[\] processlist = Process.GetProcesses();
            foreach(Process theprocess in processlist)
            {
                   Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
            }
      

      My problem is that I want to know the filename (EXE) of a process, and the pathname it was started from. For example, if there is a process Notepad.exe running, I want to determine in which folder that file actually resides, i.e. something like "C:\Windows\system32\notepad.exe". How can I get that information? I already tried to get the member StartInfo. but it doesn't contain a link to the folder. How can I do this?

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #3

      You can also use

      theprocess.Modules[indexno].FileName // here indexno value of modules in the collection

      :)

      L M 2 Replies Last reply
      0
      • L Lost User

        theprocess.MainModule.FileName

        R Offline
        R Offline
        RaviRanjanKr
        wrote on last edited by
        #4

        qwertz321 wrote:

        theprocess.MainModule.FileName

        Good one My vote 5. :)

        1 Reply Last reply
        0
        • R RaviRanjanKr

          You can also use

          theprocess.Modules[indexno].FileName // here indexno value of modules in the collection

          :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #5

          that is much more expensive and in no way any better than what qwertz already provided. :|

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          R 1 Reply Last reply
          0
          • L Luc Pattyn

            that is much more expensive and in no way any better than what qwertz already provided. :|

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            R Offline
            R Offline
            RaviRanjanKr
            wrote on last edited by
            #6

            Luc Pattyn wrote:

            that is much more expensive and in no way any better than what qwertz already provided.

            Yes, that's why I vote 5 for qwertz and during writing my answer I have mention you can also use and code works very well. I just try to give an alternate answer. :)

            S 1 Reply Last reply
            0
            • R RaviRanjanKr

              You can also use

              theprocess.Modules[indexno].FileName // here indexno value of modules in the collection

              :)

              M Offline
              M Offline
              MDNadeemAkhter
              wrote on last edited by
              #7

              RaviRanjankr wrote:

              theprocess.Modules[indexno].FileName

              We need to handle Exception in this case.

              R 1 Reply Last reply
              0
              • M MDNadeemAkhter

                RaviRanjankr wrote:

                theprocess.Modules[indexno].FileName

                We need to handle Exception in this case.

                R Offline
                R Offline
                RaviRanjanKr
                wrote on last edited by
                #8

                MDNadeemAkhter wrote:

                We need to handle Exception in this case.

                Yes, in both cases either you are using MainModules or Modules you need to handle exception..

                1 Reply Last reply
                0
                • R RaviRanjanKr

                  Luc Pattyn wrote:

                  that is much more expensive and in no way any better than what qwertz already provided.

                  Yes, that's why I vote 5 for qwertz and during writing my answer I have mention you can also use and code works very well. I just try to give an alternate answer. :)

                  S Offline
                  S Offline
                  ShilpaKumari
                  wrote on last edited by
                  #9

                  Good its just look like a fire A complete + Answer :)

                  R 1 Reply Last reply
                  0
                  • S ShilpaKumari

                    Good its just look like a fire A complete + Answer :)

                    R Offline
                    R Offline
                    RaviRanjanKr
                    wrote on last edited by
                    #10

                    ShilpaKumari wrote:

                    Good its just look like a fire A complete + Answer

                    Not a Matter some people Down vote or comment without testing code, which is not good in any corner. :)

                    A 1 Reply Last reply
                    0
                    • R RaviRanjanKr

                      ShilpaKumari wrote:

                      Good its just look like a fire A complete + Answer

                      Not a Matter some people Down vote or comment without testing code, which is not good in any corner. :)

                      A Offline
                      A Offline
                      AshishKr
                      wrote on last edited by
                      #11

                      RaviRanjankr wrote:

                      some people Down vote or comment without testing code, which is not good in any corner.

                      agree with you

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups