Help me to avoid again & again changing the path...........

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mahyog
    New Member
    • Sep 2010
    • 9

    Help me to avoid again & again changing the path...........

    Hi,
    I want to change the BackgroundImage of form and i have created the folder named Images in my project and i will change the BackgroundImage when user press any key and I try it on different PC & always I have to change the path of it, so any other method anyone know to keep path constant then please tell me....

    Code:
    BackgroundImage = Image.FromFile("C:/Users/HOME/Documents/Visual Studio 2008/Projects/Patient Testing/Patient Testing/Images/" + pic[count].ToString() + ".jpg");
    Thank you...
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    This should do the trick:

    Code:
    string AppPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Images\\");
                MessageBox.Show(AppPath);
    Remember to add using System.IO to the top of your code.

    Aimee.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      For the most part: If the images directory is in the same place as the exe, using relative paths might work.
      Otherwise consider:
      string dir=Path.Combin e(AppDomain.Cur rentDomain.Base Directory,"Imag es");

      Comment

      • Mahyog
        New Member
        • Sep 2010
        • 9

        #4
        Thanx but it gives me the path

        Code:
        C:\Users\HOME\Documents\Visual Studio 2008\Projects\Patient Testing\Patient Testing\bin\Debug\Images\4.jpg
        but I want path.

        Code:
        C:\Users\HOME\Documents\Visual Studio 2008\Projects\Patient Testing\Patient Testing\Images\4.jpg

        Comment

        • Christian Binder
          Recognized Expert New Member
          • Jan 2008
          • 218

          #5
          Try the following^:

          Path.Combine(Ap pDomain.Current Domain.BaseDire ctory, @"..\..\Images" );

          Comment

          • Mahyog
            New Member
            • Sep 2010
            • 9

            #6
            This also not working...

            Code:
            C:\Users\HOME\Documents\Visual Studio 2008\Projects\Patient Testing\Patient Testing\bin\Debug\Images\4.jpg
            I dont want to go in bin and Debug folder my Images folder is in following way...

            Code:
            C:\Users\HOME\Documents\Visual Studio 2008\Projects\Patient Testing\Patient Testing\Images\4.jpg

            Comment

            • Christian Binder
              Recognized Expert New Member
              • Jan 2008
              • 218

              #7
              My suggestion would/should create the following:
              Code:
              C:\Users\HOME\Documents\Visual Studio 2008\Projects\Patient Testing\Patient Testing\bin\Debug\..\..\Images\4.jpg
              If you open this path, it would open the file
              Code:
              C:\Users\HOME\Documents\Visual Studio 2008\Projects\Patient Testing\Patient Testing\Images\4.jpg
              because "..\" means one folder up in hierarchy.

              Alternatively you could do the following:
              Code:
              string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Images"); 
              path = path.Replace(@"\bin\Debug", "");

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Your project will never work if you don't tell visual studio that your images directory is content to be copied to the ouput directory (and thus, part of the project). When you get that fixed, you can use the code I provided.

                Comment

                • Subin Ninan
                  New Member
                  • Sep 2010
                  • 91

                  #9
                  Keep your images in same directory where your application.exe exists and just specify image filename in your code.
                  For example:
                  BackgroundImage = "xyz.jpg";

                  Comment

                  Working...