none
C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE and Application.StartupPath in UserControl RRS feed

  • Question

  • Hello

    I have a UserControl. 

    I have a  "string pictureBookPath".

    I write "pictureBookPath = Application.StartupPath + @"\Book\" + "1" + ".gif";" but I have this Error:

    C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\1.gif;

    1.gif is in bin folder.


    • Edited by ARZARE Tuesday, June 2, 2015 11:06 PM
    Tuesday, June 2, 2015 10:57 PM

Answers

  • You should embed the gif in the UserControl as a resource as was noted. Then you won't need a file path.

    This KB is a step-by-step for embedding a resource and accessing it in WinForms:

    https://support.microsoft.com/en-us/kb/319292

    You should end up with something like:

            private void button1_Click(object sender, EventArgs e)
            {
                System.Reflection.Assembly _assembly;
                System.IO.Stream _imageStream;
    
                _assembly = System.Reflection.Assembly.GetExecutingAssembly();
                _imageStream = _assembly.GetManifestResourceStream("WindowsFormsControlLibrary1.1.gif");
    
                this.pictureBox1.Image = new Bitmap(_imageStream);
            }

    Thursday, June 4, 2015 12:32 AM

All replies

  • Hi,
    Would you show us just your code without abbreviation?
      i.e. only variable and statement related to the issue
    and tell us your language.
    Tuesday, June 2, 2015 11:56 PM
  • A user control should not assume what the startup path is. E.g. when being designed in Visual Studio, the application path is Visual Studio's folder not your executable's.

    Usually you embed the picture resource right into the assembly. Assembly.GetExecutingAssembly will give you the dll path if you want to have a separate file.




    Visual C++ MVP

    Wednesday, June 3, 2015 1:21 AM
    Moderator
  • Hello

    Thank you very much

    I wrote this code:

                ResourceManager rm = new ResourceManager("imageResources", Assembly.GetExecutingAssembly());            
                Bitmap b = new Bitmap(treeViewAndChapter.Properties.Resources._1);
                pbBook.Image = (Image)b;
                pbBook.SizeMode = PictureBoxSizeMode.StretchImage;

    Now how can I get path as string? I want "string path".

    Wednesday, June 3, 2015 9:38 AM
  • Hello,
    Where's your code  "pictureBookPath = Application.StartupPath + @"\Book\" + "1" + "?
    I'd like to see it and a part of declaration of "
    pictureBookPath".

      I wrote before ...
         "i.e. only variable and statement related to the issue

         and tell us your language."

    sorry my poor English (I'm from Japan)
    Wednesday, June 3, 2015 10:52 AM
  • "1.gif" is in bin/Debug
    Wednesday, June 3, 2015 2:39 PM
  • Hello, ARZARE,

    I'm very sorry.   I have no confidence to communicate to you.

    Best regards,

    Wednesday, June 3, 2015 11:17 PM
  • You should embed the gif in the UserControl as a resource as was noted. Then you won't need a file path.

    This KB is a step-by-step for embedding a resource and accessing it in WinForms:

    https://support.microsoft.com/en-us/kb/319292

    You should end up with something like:

            private void button1_Click(object sender, EventArgs e)
            {
                System.Reflection.Assembly _assembly;
                System.IO.Stream _imageStream;
    
                _assembly = System.Reflection.Assembly.GetExecutingAssembly();
                _imageStream = _assembly.GetManifestResourceStream("WindowsFormsControlLibrary1.1.gif");
    
                this.pictureBox1.Image = new Bitmap(_imageStream);
            }

    Thursday, June 4, 2015 12:32 AM