Question Access to Path Denied

Skullptor

New member
Joined
Dec 12, 2016
Messages
2
Programming Experience
3-5
I have a program written in C# using visual studio 2022. I'm trying to use File Stream to display a picture on my form. I keep getting an error message Access to the path 'G:\Train Inventory' is denied.' I've researched the web to no avail. I've set the permissions, sharing, etc., all to no avail. Here is a small snippet of my code
C#:
String FilePath = "G:\\Train Inventory";
// Example: Create a MemoryStream from an existing image file
var imagePath = FilePath;
var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read); \\this line throws the error
var memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
 
Last edited by a moderator:
Is "G:\Train Inventory" actually a directory rather than a file?

If it is truly a file, it's kind of unusual that it doesn't have a file extension. Most images would have a file extension like .bmp, .gif, .png, or .jpg.

The next thing to look at is if you have any antivirus software running. And if so, did you tell it to ignore or your application? Some AV software will block unknown applications from access the file system.

And then the last thing to look is is if another application has the file locked open with an exclusive lock. As long as that exclusive lock is on the file, then other programs won't be able to access it.
 
And then the last thing to look is is if another application has the file locked open with an exclusive lock. As long as that exclusive lock is on the file, then other programs won't be able to access it.

Although I think that would result in a different error message.
 
Although I think that would result in a different error message.

I was using FromFile but it was locking the file. In searching the internet, it was recommended to use File Stream. I don't save the image itself in the database because it consumes too much space so I save the path to the image which includes the .jpg extension. Because the file holds different items I use a variable to hold the path to the image.
 
Let me understand. If you were using FromFile(), you weren't getting the access denied error, but had the unintended consequence of locking the file? If so, then it's just a matter of disposing the Image when you are done with it so that the file gets unlocked.

Furthermore, if you were storing the path in a variable, and you passed that variable to FromFile(), then you could also remove the code that uses FromFile() and instead pass that variable to the FileStream constructor, or alternately to File.OpenRead(). If FromFile() wasn't getting access denied, then neither should the FileStream.
 
There's a lot going on here. @Skydiver has already addressed most of it but let's take one issue at a time.

Firstly, the issue you specifically asked about here, i.e. access denied on the specified path, is almost certainly because you have specified a folder path rather than a file path, as already indicated. You say that the file path has a ".jpg" extension but you haven't included that in the path you showed, so you have omitted that at the very least. Just that would result in the file not being found though, so you have almost certainly provide a path to the folder containing the file, rather than the path of the file. If Image.FromFile was working with the path you were using, the code you posted will work with the same path.

As already suggested, if you call Dispose on an Image object that was created from Image.FromFile then that will release the file it was created from. That's all you need to do if you don't need to modify that file in any way until after you've finished showing the image. If you need to modify it sooner though, you will need to take a different approach in order to release the file sooner.

If you are trying to create an Image from a Stream, probably by calling Image.FromStream, then that MemoryStream is pointless. A FileStream is a Stream too, so you can just create the Image object from that.
C#:
Image img;

using (var fs = new FileStream(...))
{
    img = Image.FromStream(fs);
}
You might not even need the Image variable, as you can use the result of Image.FromStream right there in the using block.

You probably don't even need to create a Stream yourself though. It sounds like you're using a WinForms PictureBox, so you can just assign the file path to the ImageLocation property or pass it to the Load method. If memory serves, at least one (both, I think) of those two will display the image without locking the file.
 

Latest posts

Back
Top Bottom