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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. location of current document

location of current document

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 2 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 Offline
    E Offline
    edel_ong
    wrote on last edited by
    #1

    Hi, I'm a newbie at C#, I was wondering if there was a code for knowing the location of the current open C# document. Let's say I want to know the location(In string form) of Form1.cs, is there any code to represent this? Thank you very much :)

    J 1 Reply Last reply
    0
    • E edel_ong

      Hi, I'm a newbie at C#, I was wondering if there was a code for knowing the location of the current open C# document. Let's say I want to know the location(In string form) of Form1.cs, is there any code to represent this? Thank you very much :)

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Can you be more specific? The opened C# file...are you trying to find out the file path of a C# file Visual Studio has opened?

      E 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Can you be more specific? The opened C# file...are you trying to find out the file path of a C# file Visual Studio has opened?

        E Offline
        E Offline
        edel_ong
        wrote on last edited by
        #3

        Yes. Because I want to access a folder that is located in the same folder as the file but I can't seem to access it. it looks like this: Folder1 contains -Form1.cs -FolderIwantToAccess contains pic.bmp I want to access pic.bmp from Form1.cs Thanks a lot! -- modified at 22:35 Monday 6th February, 2006

        J 1 Reply Last reply
        0
        • E edel_ong

          Yes. Because I want to access a folder that is located in the same folder as the file but I can't seem to access it. it looks like this: Folder1 contains -Form1.cs -FolderIwantToAccess contains pic.bmp I want to access pic.bmp from Form1.cs Thanks a lot! -- modified at 22:35 Monday 6th February, 2006

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Your .cs file doesn't execute directly. It gets compiled into your .exe file located in your \bin\debug\ directory. So your C# code will want to locate the bitmap relative to that directory. If your directory structure is

          MyProject
          -FolderIWantToAccess
          -bin
          -Debug

          Then you can access the image from your app using "..\\..\\FolderIWantToAccess\\pic.bmp"

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

          E 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Your .cs file doesn't execute directly. It gets compiled into your .exe file located in your \bin\debug\ directory. So your C# code will want to locate the bitmap relative to that directory. If your directory structure is

            MyProject
            -FolderIWantToAccess
            -bin
            -Debug

            Then you can access the image from your app using "..\\..\\FolderIWantToAccess\\pic.bmp"

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

            E Offline
            E Offline
            edel_ong
            wrote on last edited by
            #5

            I tried it but the program still doesnt read the picture file...I want to access the pic.bmp without directly enforcing the complete filepath (e.g. c:\mydocuments\sample\project\folderiwanttoaccess\pic.bmp <--i dont want to write this in the program source file...)

            J 1 Reply Last reply
            0
            • E edel_ong

              I tried it but the program still doesnt read the picture file...I want to access the pic.bmp without directly enforcing the complete filepath (e.g. c:\mydocuments\sample\project\folderiwanttoaccess\pic.bmp <--i dont want to write this in the program source file...)

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              I understand what you're trying to do. You can access files in a higher directory level that the exe using "..\\". So if your .exe file is running from c:\mydocuments\sample\project\bin\debug\test.exe, you can locate the image from your C# code using // Adjust the line below depending on how many parent directories must be navigated upwards to read the folderIWantAccessTo directory. stromg pathToImage = "..\\..\\..\\folderIWantAccessTo\pic.bmp"; If you want to see what's going on with those "..\\" things, see what this returns: string path = Path.GetFullPath("..\\..\\..\\"); It should return your project root. From there the rest is easy, just append the folderIWantAccessTo\pic.bmp to the end of the string and you're good.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

              E 1 Reply Last reply
              0
              • J Judah Gabriel Himango

                I understand what you're trying to do. You can access files in a higher directory level that the exe using "..\\". So if your .exe file is running from c:\mydocuments\sample\project\bin\debug\test.exe, you can locate the image from your C# code using // Adjust the line below depending on how many parent directories must be navigated upwards to read the folderIWantAccessTo directory. stromg pathToImage = "..\\..\\..\\folderIWantAccessTo\pic.bmp"; If you want to see what's going on with those "..\\" things, see what this returns: string path = Path.GetFullPath("..\\..\\..\\"); It should return your project root. From there the rest is easy, just append the folderIWantAccessTo\pic.bmp to the end of the string and you're good.

                Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

                E Offline
                E Offline
                edel_ong
                wrote on last edited by
                #7

                I figured out what was lacking in your idea... there should be three "." instead of two. It is now working correctly. Thanks very much :)

                J 1 Reply Last reply
                0
                • E edel_ong

                  I figured out what was lacking in your idea... there should be three "." instead of two. It is now working correctly. Thanks very much :)

                  J Offline
                  J Offline
                  Judah Gabriel Himango
                  wrote on last edited by
                  #8

                  I'm glad you figured it out.

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

                  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