I have a c# application which shows images in a form. I'm trying to overwrite these images and regenerate them. I get an exception while I'm trying to delete an existing image (it's a png in this case). I tried disposing the image that the picturebox is using and then setting it to null, but I still get an exception due to a sharing violation. Nevertheless, I can go to explorer and delete the file without any problem.
In trying to figure out what process has this image locked, Process Monitor tells me it's the vhost.exe which is hosting my application.
How can I get around this? Is there some way I can get the host to release the lock on the file so that I can delete/recreate it? Ultimately I have large number of images which are generated as thumbnails which would need updating anytime my database incurs changes which affect the graphics. I'd hate to think I need to call a command shell to do this.
Thanks for any advice.
Gary
You can try loading images using file streams and closing them after reading.
FileStream fileStream = new FileStream("ImageName.jpg", FileMode.Open, FileAccess.Read);
yourPictureBox.Image = Image.FromStream(fileStream);
fileStream.Close();
Or instead of explicitly closing; you can employ using statement;
using(FileStream fileStream = new FileStream("ImageName.jpg", FileMode.Open, FileAccess.Read))
{
yourPictureBox.Image = Image.FromStream(fileStream);
}
Related
My program opens a file for reading purposes and does not let other programs access its contents. This is exactly what I want to do, except another specific process also needs to read the file. I do not manage this process, so that would be ideal if I could define it by its name or PID.
What I have done:
FileStream fileStream = new FileStream(FILE, FileMode.Open, FileAccess.Read, FileShare.None);
The problem with what I did, as previously stated, was it won't allow the other process to read from it. I know I could just have it close the FileStream, use File. Read, or change FileShare, but I only one my process and the other process to read the file.
I've looked at several solutions to reading a file that's already in use by another process, but none of them seem to work for me.
The file I'm trying to read is an XML file that contains configuration settings that I need to extract.
Here's what I have tried:
using (var stream = File.Open("\\\\2008r2\\c$\\ProgramData\\location\\siteConfig.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = new StreamReader(stream))
{
// Actions you perform on the reader.
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}
This seems to work for just about everyone else, I don't know what I'm doing wrong! Is my file locked in a different manner and cannot be accessed even to read?
Help much appreciated!
Dave
From your comment, the original process has opened the file with FileShare.None. From MSDN:
Declines sharing of the current file. Any request to open the file (by
this process or another process) will fail until the file is closed.
The original process has an exclusive lock on it so you won't be able to read from it unless the FileShare enumeration is changed from None or the file is closed.
I am describing my work process bellow:
I get image files from a directory.
Creating a PictureBox array for displaying the images.
Creating a Image array from the files that I got from the directory. I am creating this array for making the image source of PictureBox.
I am copying the files to another directory. By this:
File.Copy(allFiles[i],fullPath+fullName+"-AA"+nameString+ext);
Now I want to delete the files from the directory. For this I am doing this:
File.Delete(allFiles[i]);
But its giving me this error:
The process cannot access the file 'C:\G\a.jpg' because it is being used by another process.
Please tell me how to resolve this? I didn't attach the full code here because it'll be large. Please ask me if you want to see any part of my code.
Chances are you are loading the image directly from the file. For example,
PictureBox[i] = Image.FromFile(allFiles[i]);
If you look up the documentation for the Image.FromFile method, you will find that it actually locks the file until the Image is disposed. (In fact, most other loading methods in the Image class also locks the file until the Image is disposed.)
So to work around this problem, copy the picture file contents to memory and load it from there. For example,
PictureBox[i] = Image.FromStream(new MemoryStream(File.ReadAllBytes(allFiles[i])));
That way, the file itself will remain unlocked and you can freely move/delete it.
Of course it's being used.
Copy the files to another directory first (Step 4),
Delete the old directory
then do the third step (assigning it to a array and loading it to PictureBox) from the newly copied directory.
Alternative:
You must close the stream handler before deleting the files..
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (PictureBox[i] = Image.FromStream(fs))
{
...
}
Is there a way to read binary data from a read-only file? I have an Excel worksheet, which might be opened in Excel but I want to open it for read purposes only.
I tried to do it this way:
using (FileStream fileStream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
And I am getting
The process cannot access the file 'something.xlsx' because it is being used by another process.
Is there any way to achieve that?
Change this argument:
FileShare.Read
to this:
FileShare.ReadWrite
You are attempting to deny write access to the file, which is causing your issue as Excel already has it open for writing.
You can't open a file who are already open. Be careful to close your file after open it. And you should verify that you didn't use the file in another software (at the same time)...
I am writing to the text file some data. I am using this code:
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (TextWriter tw = new StreamWriter(fs))
{
tw.WriteLine("sample_data");
}
}
When file is opened by notepad my app can write into it. When this file is opened by MS Excel I get following error: The process cannot access the file myfile.csv because it is being used by another process. What can cause this situation and how can I solve this problem?
Notepad opens the file, reads in the entire contents and then closes the file. You can even delete the file that is open in notepad.
Excel on the other hand keeps the file open as long as it is displayed. There are some special sharing tools that can be enabled in Excel for excel format files. In that case I assume that it is opened non-exlusively. Otherwise Excel opens the file exclusively and keeps it open.
It doesn't matter that you specify a share option when opening, if the file is already opened by someone else in exclusive mode.
Excel will lock the file when it is open which prevents interaction with the file. One way I worked around this is that I wrote code to scan for excel processes on the local machine and would kill those processes before accessing a file that was open with excel. You could determine if a file is locked by looking at How to check for file lock? and then running the process killing code in the exception handler.
Open the csv file using OleDB and use INSERT and/or UPDATE statements.