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 / C++ / MFC
  4. How to find the path of my current running application

How to find the path of my current running application

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialjsonhelpquestion
16 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.
  • C Offline
    C Offline
    chandu004
    wrote on last edited by
    #1

    hai friends, my problem is like this. in my program, (say for example) i will allow the user to select a file, any where in the computer. now after file selection, i have to create one file in the folder, where my program is available. i donot give any path while creation. i will simply use open(file1,write). but this file is created, in the folder, where the selected file is available. is there any api, which can fetch me the path of the current working folder, such that, i can save my folder there? thanks for your views.

    J H N 3 Replies Last reply
    0
    • C chandu004

      hai friends, my problem is like this. in my program, (say for example) i will allow the user to select a file, any where in the computer. now after file selection, i have to create one file in the folder, where my program is available. i donot give any path while creation. i will simply use open(file1,write). but this file is created, in the folder, where the selected file is available. is there any api, which can fetch me the path of the current working folder, such that, i can save my folder there? thanks for your views.

      J Offline
      J Offline
      Jonathan Darka
      wrote on last edited by
      #2

      Try GetCurrentDirectory()


      Jonathan Wilkes Darka[Xanya.net]

      C 1 Reply Last reply
      0
      • C chandu004

        hai friends, my problem is like this. in my program, (say for example) i will allow the user to select a file, any where in the computer. now after file selection, i have to create one file in the folder, where my program is available. i donot give any path while creation. i will simply use open(file1,write). but this file is created, in the folder, where the selected file is available. is there any api, which can fetch me the path of the current working folder, such that, i can save my folder there? thanks for your views.

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #3

        Im not sure do you need to GetModuleFileName?

        C 1 Reply Last reply
        0
        • J Jonathan Darka

          Try GetCurrentDirectory()


          Jonathan Wilkes Darka[Xanya.net]

          C Offline
          C Offline
          chandu004
          wrote on last edited by
          #4

          thanks for your response. GetCurrentDirectory() gave me some clue, to achieve my goal. but it still fetches me the previous selected file's directory. how ever with GetCurrentDirectory() , in the initdialog, i can find the folder of the application and preserve it and use it though user changes the current folder. but instead of that, if i get it directly, it would be better. did you get my point or do i have to explain better?

          J 1 Reply Last reply
          0
          • H Hamid Taebi

            Im not sure do you need to GetModuleFileName?

            C Offline
            C Offline
            chandu004
            wrote on last edited by
            #5

            vov, hamid, i tried it. it gave me the filename which iam running, like, d:\myapp\debug\myapp.exe. this will do for me. what all i want from this string is upto debug. may be i can do some parsing logic here to remove the myapp.exe. i would be pleased if you can suggest that for me. got my 5? thank you.

            R H R 3 Replies Last reply
            0
            • C chandu004

              thanks for your response. GetCurrentDirectory() gave me some clue, to achieve my goal. but it still fetches me the previous selected file's directory. how ever with GetCurrentDirectory() , in the initdialog, i can find the folder of the application and preserve it and use it though user changes the current folder. but instead of that, if i get it directly, it would be better. did you get my point or do i have to explain better?

              J Offline
              J Offline
              Jonathan Darka
              wrote on last edited by
              #6

              Here you go, this should do what you want.

              TCHAR szBuffer[_MAX_PATH] = {'\0'};
              DWORD dwLen = _MAX_PATH;

              GetModuleFileName(GetModuleHandle(NULL), szBuffer, dwLen);


              Jonathan Wilkes Darka[Xanya.net]

              C 1 Reply Last reply
              0
              • C chandu004

                vov, hamid, i tried it. it gave me the filename which iam running, like, d:\myapp\debug\myapp.exe. this will do for me. what all i want from this string is upto debug. may be i can do some parsing logic here to remove the myapp.exe. i would be pleased if you can suggest that for me. got my 5? thank you.

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                See PathRemoveFileSpec()

                Real men drive manual transmission.

                1 Reply Last reply
                0
                • C chandu004

                  vov, hamid, i tried it. it gave me the filename which iam running, like, d:\myapp\debug\myapp.exe. this will do for me. what all i want from this string is upto debug. may be i can do some parsing logic here to remove the myapp.exe. i would be pleased if you can suggest that for me. got my 5? thank you.

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  Yeah you can use of CString::Left for get full string of some strings of left. (one thing that it doesnt relative to your answer see my thread on the lonuge I saw a good site with nice wallpaper of milky way )

                  C 1 Reply Last reply
                  0
                  • C chandu004

                    vov, hamid, i tried it. it gave me the filename which iam running, like, d:\myapp\debug\myapp.exe. this will do for me. what all i want from this string is upto debug. may be i can do some parsing logic here to remove the myapp.exe. i would be pleased if you can suggest that for me. got my 5? thank you.

                    R Offline
                    R Offline
                    Roger Stoltz
                    wrote on last edited by
                    #9

                    _splitpath[^] in the CRT lib.


                    "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                    "High speed never compensates for wrong direction!" - unknown

                    C 1 Reply Last reply
                    0
                    • H Hamid Taebi

                      Yeah you can use of CString::Left for get full string of some strings of left. (one thing that it doesnt relative to your answer see my thread on the lonuge I saw a good site with nice wallpaper of milky way )

                      C Offline
                      C Offline
                      chandu004
                      wrote on last edited by
                      #10

                      nice of ur wallpapers. why don't you attempt for my algorithm in corresponding forum.

                      H 1 Reply Last reply
                      0
                      • C chandu004

                        nice of ur wallpapers. why don't you attempt for my algorithm in corresponding forum.

                        H Offline
                        H Offline
                        Hamid Taebi
                        wrote on last edited by
                        #11

                        Im sorry but I dont understand what you said?

                        C 1 Reply Last reply
                        0
                        • J Jonathan Darka

                          Here you go, this should do what you want.

                          TCHAR szBuffer[_MAX_PATH] = {'\0'};
                          DWORD dwLen = _MAX_PATH;

                          GetModuleFileName(GetModuleHandle(NULL), szBuffer, dwLen);


                          Jonathan Wilkes Darka[Xanya.net]

                          C Offline
                          C Offline
                          chandu004
                          wrote on last edited by
                          #12

                          no boss i'm still getting the recent selected file's folder by the user. thanks for your concern.

                          T 1 Reply Last reply
                          0
                          • H Hamid Taebi

                            Im sorry but I dont understand what you said?

                            C Offline
                            C Offline
                            chandu004
                            wrote on last edited by
                            #13

                            i mean please go to algorithms forum and try to attempt my post. thanku

                            1 Reply Last reply
                            0
                            • R Roger Stoltz

                              _splitpath[^] in the CRT lib.


                              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                              "High speed never compensates for wrong direction!" - unknown

                              C Offline
                              C Offline
                              chandu004
                              wrote on last edited by
                              #14

                              thanks for your reply roger. i shall definitely look into it if all my other choices disappoint me.

                              1 Reply Last reply
                              0
                              • C chandu004

                                no boss i'm still getting the recent selected file's folder by the user. thanks for your concern.

                                T Offline
                                T Offline
                                ThatsAlok
                                wrote on last edited by
                                #15

                                is you application is executable or dll!

                                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                                Never mind - my own stupidity is the source of every "problem" - Mixture

                                cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                                1 Reply Last reply
                                0
                                • C chandu004

                                  hai friends, my problem is like this. in my program, (say for example) i will allow the user to select a file, any where in the computer. now after file selection, i have to create one file in the folder, where my program is available. i donot give any path while creation. i will simply use open(file1,write). but this file is created, in the folder, where the selected file is available. is there any api, which can fetch me the path of the current working folder, such that, i can save my folder there? thanks for your views.

                                  N Offline
                                  N Offline
                                  Nelek
                                  wrote on last edited by
                                  #16

                                  Try with...

                                  CString m_szNewFileName = "";
                                  char FileName[MAX_PATH];
                                  GetModuleFileName(NULL, FileName, MAX_PATH);
                                  m_szNewFileName = FileName;
                                  m_szNewFileName = m_szNewFileName.Left(m_szNewFileName.ReverseFind('\\'));
                                  m_szNewFileName += "DesiredNewName.ext"

                                  CFile f (m_szNewFileName, CFile::modeCreate | CFile::modeWrite);

                                  it worked good for me. This would create the new file in the forlder where the App.exe is. And afterwards you can delete the file with:

                                  if (f.Open (m_szNewFileName, CFile::modeRead))
                                  { f.Close ();
                                  f.Remove (m_szNewFileName);
                                  }

                                  EDIT: Nice, my post is the newest one, but has the answers of the post just above (older than mine):doh: :P -- modified at 5:21 Monday 29th October, 2007 -- modified at 5:22 Monday 29th October, 2007

                                  Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to 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