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. Alternative to Application.StartupPath

Alternative to Application.StartupPath

Scheduled Pinned Locked Moved C#
csharphelpquestion
18 Posts 4 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.
  • M MichCl

    I have a class that gets it's startup path using the following:

    String myPath = Application.StartupPath;

    This worked fine with it's own test form, but once I was referring to this class from another class in a different directory, it returns the wrong directory. Is there a c# way to get the directory of the class and not the class that calls it? Thanks for any help!

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

    This has nothing to do with classes, it returns the path to the executable program as described here[^].

    speaking as ...

    1 Reply Last reply
    0
    • L Lost User

      Console.Write(typeof(string).Assembly.Location);

      Will the path, prolly including the assemblies name.

      Bastard Programmer from Hell :suss:

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #4

      Uh, surely that will tell you where mscorlib is?

      L 1 Reply Last reply
      0
      • M MichCl

        I have a class that gets it's startup path using the following:

        String myPath = Application.StartupPath;

        This worked fine with it's own test form, but once I was referring to this class from another class in a different directory, it returns the wrong directory. Is there a c# way to get the directory of the class and not the class that calls it? Thanks for any help!

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #5

        Assembly.GetExecutingAssembly().Location.

        L 1 Reply Last reply
        0
        • B BobJanova

          Uh, surely that will tell you where mscorlib is?

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

          Yes. Do you think I should have said that I used "string" as an example type?

          Bastard Programmer from Hell :suss:

          1 Reply Last reply
          0
          • B BobJanova

            Assembly.GetExecutingAssembly().Location.

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

            Surely, that returns the location of the executing assembly? :-D

            TopicStarter wrote:

            Is there a c# way to get the directory of the class and not the class that calls it?"

            Bastard Programmer from Hell :suss:

            B 1 Reply Last reply
            0
            • L Lost User

              Surely, that returns the location of the executing assembly? :-D

              TopicStarter wrote:

              Is there a c# way to get the directory of the class and not the class that calls it?"

              Bastard Programmer from Hell :suss:

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #8

              Yes, but 'the class' surely refers to the current class, which will be in the executing assembly. That's how I read it, anyway.

              L M 2 Replies Last reply
              0
              • B BobJanova

                Yes, but 'the class' surely refers to the current class, which will be in the executing assembly. That's how I read it, anyway.

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

                Yup, sounds logical too. It must be either time for coffee, or for some sleep :)

                Bastard Programmer from Hell :suss:

                1 Reply Last reply
                0
                • M MichCl

                  I have a class that gets it's startup path using the following:

                  String myPath = Application.StartupPath;

                  This worked fine with it's own test form, but once I was referring to this class from another class in a different directory, it returns the wrong directory. Is there a c# way to get the directory of the class and not the class that calls it? Thanks for any help!

                  M Offline
                  M Offline
                  MichCl
                  wrote on last edited by
                  #10

                  To clarify: main program is in directory A main program uses sub-program in directory B I need to get the directory of B programatically. Wouldn't anything referring to executing assembly refer to the location of main program in directory A? Since I am referring to program B in program A, the dll or exe is copied to program A's directory, so getting that location would return program A's location, not program B. I have a file I need to use/get in dir B. A->B and B is trying to open a file in it's directory but I'm getting the location of A because A is the application startup path (and executing assembly,no?). Sorry for the confusion! I hope that this makes it more clear!

                  C L 2 Replies Last reply
                  0
                  • B BobJanova

                    Yes, but 'the class' surely refers to the current class, which will be in the executing assembly. That's how I read it, anyway.

                    M Offline
                    M Offline
                    MichCl
                    wrote on last edited by
                    #11

                    Thanks for the input, guys. See my clarification at the end here. :)

                    1 Reply Last reply
                    0
                    • M MichCl

                      To clarify: main program is in directory A main program uses sub-program in directory B I need to get the directory of B programatically. Wouldn't anything referring to executing assembly refer to the location of main program in directory A? Since I am referring to program B in program A, the dll or exe is copied to program A's directory, so getting that location would return program A's location, not program B. I have a file I need to use/get in dir B. A->B and B is trying to open a file in it's directory but I'm getting the location of A because A is the application startup path (and executing assembly,no?). Sorry for the confusion! I hope that this makes it more clear!

                      C Offline
                      C Offline
                      Cracked Down
                      wrote on last edited by
                      #12

                      Is directory of the B is relative to the A's directory then you can calculate the path for B's directory get the App path and then add directories or remove directories depending on the B's location... Assuming that B's directory does not change with respect to A's Happy Coding :)

                      M 1 Reply Last reply
                      0
                      • M MichCl

                        To clarify: main program is in directory A main program uses sub-program in directory B I need to get the directory of B programatically. Wouldn't anything referring to executing assembly refer to the location of main program in directory A? Since I am referring to program B in program A, the dll or exe is copied to program A's directory, so getting that location would return program A's location, not program B. I have a file I need to use/get in dir B. A->B and B is trying to open a file in it's directory but I'm getting the location of A because A is the application startup path (and executing assembly,no?). Sorry for the confusion! I hope that this makes it more clear!

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

                        As I pointed out earlier this command returns the path of the executable program. It does not matter where the various components used to build that executable are stored.

                        speaking as ...

                        M 1 Reply Last reply
                        0
                        • C Cracked Down

                          Is directory of the B is relative to the A's directory then you can calculate the path for B's directory get the App path and then add directories or remove directories depending on the B's location... Assuming that B's directory does not change with respect to A's Happy Coding :)

                          M Offline
                          M Offline
                          MichCl
                          wrote on last edited by
                          #14

                          That's a good idea. A's directory is blah/blah1/blah2/tool/tool/bin/Debug and then B's directory could be found by appending it's location onto blah/blah1/blah2.

                          1 Reply Last reply
                          0
                          • L Lost User

                            As I pointed out earlier this command returns the path of the executable program. It does not matter where the various components used to build that executable are stored.

                            speaking as ...

                            M Offline
                            M Offline
                            MichCl
                            wrote on last edited by
                            #15

                            But B's exe file is referenced by program A, so A copies the exe file to A's Debug directory. So getting the exe location would return A's dir and not B's.

                            L 1 Reply Last reply
                            0
                            • M MichCl

                              But B's exe file is referenced by program A, so A copies the exe file to A's Debug directory. So getting the exe location would return A's dir and not B's.

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

                              Exactly, and that is why it does not work. When you start an executable program Windows makes a note of its location and it is that location that will be returned. Program A can only ever find program A's directory. Maybe if you explain what problem you are actually trying to solve people will be able to give some better suggestions.

                              speaking as ...

                              M 1 Reply Last reply
                              0
                              • M MichCl

                                I have a class that gets it's startup path using the following:

                                String myPath = Application.StartupPath;

                                This worked fine with it's own test form, but once I was referring to this class from another class in a different directory, it returns the wrong directory. Is there a c# way to get the directory of the class and not the class that calls it? Thanks for any help!

                                M Offline
                                M Offline
                                MichCl
                                wrote on last edited by
                                #17

                                Hi guys. I'm going to call this since I decided what I'm going to do. For anyone looking at this post in the future, this is what I figured out. I'm new to c# programming (3 months experience), so this is my first program (in this language and with visual studio). Apparently, once this is released, all of the executables will be put in the same directory as the calling application directory, so the debug subdirectories will be gone. So I need to put my file referenced by B into A's directory. Thanks for all the help!! :) :^) Have a great Memorial Day weekend and enjoy!

                                1 Reply Last reply
                                0
                                • L Lost User

                                  Exactly, and that is why it does not work. When you start an executable program Windows makes a note of its location and it is that location that will be returned. Program A can only ever find program A's directory. Maybe if you explain what problem you are actually trying to solve people will be able to give some better suggestions.

                                  speaking as ...

                                  M Offline
                                  M Offline
                                  MichCl
                                  wrote on last edited by
                                  #18

                                  Right. It does not work. Thanks for helping!

                                  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