using Windows 7 and calling:

OpenFileDialog LogoLoad = new OpenFileDialog();

            LogoLoad.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            LogoLoad.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
            LogoLoad.Title = "Load Dealer Logo";
            LogoLoad.FilterIndex = 1;
            LogoLoad.RestoreDirectory = true;

            if (LogoLoad.ShowDialog() == DialogResult.OK)
            {
                filenname = Path.GetFileName(LogoLoad.FileName);
                path = Path.GetDirectoryName(LogoLoad.FileName);
                extension = Path.GetExtension(LogoLoad.FileName);

Problem is picturebox1.ImageLocation comes up as Null even though picture is displayed and path+filename does not give correct path IE..
It says c:\Users\username\Pictures\logo.gif
when it is actually c:\Users\username\My Pictures\Pictures\logo.gif

any clues here... cant wrap my mind why it would be giving wrong path yet displaying proper picture

Recommended Answers

All 4 Replies

OK it seems it was not inserting the \ between path and filename

added (path + "\\" + filename) and works

hope this helps someone else in the future

Hi,

Why not give a try with following line;

pictureBox1.ImageLocation = LogoLoad.FileName;

I tried with your code. After using the above line image is displayed in the picturebox and imagelocation gives the proper path.

Thank you

commented: your method worked like a charm too +2

I will try your method too :)

You should use Path.Combine() when joining a path string. Be sure to watch out for directories ending with a trailing path delimiter too. The OpenFileDialog is good about well-formed filenames but if you start talking to the registry something you will pull paths back that look like C:program Files\someDir (notice there is no blackslash in C:\).

I forget where I found this code but I didn't author it:

private string GetProgramFilesDir()
        {
            // Environment.GetFolderPath() has a bug in that it will freak out when %PROGRAMFILES% is set
            // to something like "D:" (note the lack of a backslash).
            string path = NativeMethods.SHGetFolderPath((int)(NativeConstants.CSIDL_PROGRAM_FILES | NativeConstants.CSIDL_FLAG_CREATE));

            if (path.Length == 2 && path[1] == ':')
            {
                path = path + Path.DirectorySeparatorChar;
            }

            return path;
        }

Then another:

public static string IncludeTrailingPathDelimiter(string path)
    {
      if (string.IsNullOrEmpty(path))
        return string.Empty;
      else
        return (path.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString())) ? path : path + System.IO.Path.DirectorySeparatorChar;
    }

    private void button6_Click(object sender, EventArgs e)
    {
      string dir = IncludeTrailingPathDelimiter(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
      string appName = System.IO.Path.GetFileName(Application.ExecutablePath);
      string s = System.IO.Path.Combine(dir, appName);
      System.Diagnostics.Debugger.Break();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.