C# Get image from Network Drive

I am trying to load an image located on a shared folder, on a mapped drive, into a picture box.
File.Exists always returns false even when the file exists.
Can anyone tell me why? I have given everyone Full access to the share.

private void AssignRunnersToPictureBoxes(EventDetails race)
        {
            String filePath = "I:\\";
            if (race != null)
            {
                for (int i = 1; i < 25; i++)
                {
                    PictureBox box = GetPictureBox(i);
                    if (box != null)
                    {
                        RunnerDetail runner = race.runners.GetRunner(i);
                        if (runner == null)
                        {
                            box.Tag = null;
                            box.Hide();
                        }
                        else
                        {
                            box.Tag = runner;
                       //     box.BackColor = Color.Green;
                          //  String silksFile = Path.Combine(filePath, "USA_Raci_3FBA615C103B385F0A7732B2D71BD1C8.png.bmp");
                            String silksFile = Path.Combine(filePath, runner.silkFileName + ".bmp");

                            if (File.Exists(@Path.Combine(filePath, runner.silkFileName + ".bmp")))
                            {
                                box.Image = Image.FromFile(silksFile);
                            }
                            
                            box.Show();

                        }
                    }
                }
            }

        }

Open in new window

LVL 1
jetbetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
String silksFile = Path.Combine(filePath, runner.silkFileName + ".bmp"); // add entension here only
if (File.Exists(@Path.Combine(filePath, runner.silkFileName + ".bmp")))  // or add the extension here only otherwise it is doubled  i..e filename.bmp.bmp

you seem to have a preoccupation with adding + ".bmp" everywhere!

String silksFile = Path.Combine(filePath, "USA_Raci_3FBA615C103B385F0A7732B2D71BD1C8.png.bmp");

// inconsistent filename string name -- silksfile AND silkFileName
0
jetbetAuthor Commented:
The file is a converted png that has the .bmp added so it is  blahblahblah.png.bmp

The name stored in the object (runner) does not have this extra extention added on.

SilksFile is a local String variable silkFileName is a property on the object.

I am unsure of the solution you are proposing
0
BakuRetsu_XCommented:
Print debug the full path after it has been combined.

String silksFile = Path.Combine(filePath, runner.silkFileName + ".bmp");

What does silksFile look like?
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jetbetAuthor Commented:
The path is correct. I have checked this carefully in the debugger.
0
d_yorkCommented:
While it looks on the surface like it should work straight up, there's really a lot going on under the hood that could break it.
I think what you're seeing is kind of the wonky behavior of Path.Combine() along with the wonky behavior of File.Exists().  From MSDN, if the second variable of Path.Combine appears to be an absolute path then Combine will simply return the second variable only. In fact, Path.Combine can return all sorts of stuff you might not expect.  So, for example:
//  say runner.silkFileName = "abcd"
string test = Path.Combine(filePath, runner.silkFileName + ".bmp");
// test = "I:\abcd.bmp"
//  say runner.silkFileName = "\abcd"  -- has a backslash or some kind of thing that looks like an absolute path
string test = Path.Combine(filePath, runner.silkFileName + ".bmp");
//test = "\\abcd.bmp" and File.Exists will look in the 'current working directory' for the file

Additionally, File.Exists will return false if there's any exception when it went to look for the file - including permission issue (not able to read), illegal characters, too many characters, etc..  It also returns false if it thinks the value passed is a directory.  It also looks for both relative and absolute paths.  In short, you have a lot going on with a Path.Combine inside a File.Exists - you may have to break and check your Immediate Window to see what's really going on in there.  But I'm thinking you'll need to split that up some and try different code.

Also, by default windows hides file extensions, so you may inadvertently be seeing "somename.png.bmp" but if  you go to Folder options and  uncheck "hide known extensions" you then could see "somename.png.bmp.bmp" on your I: drive.

Finally, from a permissions issue, if your application doesn't run in the context of the local user account that has permissions on the I: drive it'll get access denied and  you get no error.  So if UAC is enabled, you may have to 'run as administrator' for it to work.  Because, technically, your exe may not get executed in the context of the logged in user.

That's about it off the top of my head.  Quite a bit of things to look at really.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
Thanks for the detailed response.

I will check out your ideas as soon as I can but am in the middle of a different project at the moment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.