Hi i have the following Function:
private void CreateRoomImage(string path)
{
Directory.CreateDirectory(path);
var file = "";
foreach (PanelView panelView in pv)
{
var RoomImage = GetRaumImageName(panelView.Title);
file = path + GetImageFile(RoomImage);
if (File.Exists(file))
{
File.Delete(file);
}
using (var img = GetRaumImage(panelView.Title, panelView))
{
ImageWriter imgWriter = new ImageWriter(ImageFormat.Bmp);
imgWriter.Save(img, file);
}
}
}
My Problem is that everytime i try to Delete the existing File my programm is throwing an exception:
The process can not access the file because it is being used by another process
Is there a solution for this Problem? How can i delete the existing image?
It seems like this could be a weird race condition where the file.exists hasn't released the resource and it is trying to delete prior to release. I would probably try something along these lines.
try
{
File.Delete(file);
}
catch
{
//File does not exist or another error occurred.
}
I found it on my Own.
In PageLoad i use thet "file" also. I forgot to dispose the Image:
foreach (PanelView panel in pv)
{
path = Request.PhysicalPath.Substring(0, Request.PhysicalPath.LastIndexOf('\\') + 1) + subPath + "\\" + GetRaumImageName(panel.Title);
bitMap = new Bitmap(path + ".bmp");
b0 = BmpToMonochromConverter.CopyToBpp(bitMap, 1);
// bounce.updateInterface.UpdateProductImage(b0, panel.Panel.PRODUCT_ID, "", ref update_Handle);
bitMap.Dispose();
}
Anyway thanks for help!
Why delete file in the first place? Can't you make it overwrite the existing file if it already exists?
Another thing you can do is to pause the thread for like 400ms until OS does it's thing and files are completely deleted and all streams closed.
Related
I'm extracting a ZIP file. This ZIP contains image files and an Excel file with a product list. When articles of different sizes are listed the article refers to the same image. I copy the image file to a local folder and write the (compressed) binary data to SQL server database.
So when it gets to the point where a JPG file shall be processed a second time, I get this exception, although I dispose the image object.
Worksheet ws;
string root = "C:\\images\\";
string file;
string importFolder = "C:\\import\\;
Dictionary <string, object> ins;
Image im;
Image th;
//Worksheet has been opened before
//ZIP has been extracted before to C:\import\
for (i = 2; i <= ws.Dimension.End.Row; i++) {
ins = new Dictionary<string, object>(); //Dictionary to write data to database
file = ws.Cells[i, 4].Text;
System.IO.File.Copy(importFolder + "\\" + file, root + "\\" + file, true); // <-- Here the exception is thrown in the second iteration
im = Image.FromFile(root + "\\" + file);
im = im.GetBetterThumbnail(1024);
byte[] im_data = im.GetJpgByteArray(85);
ins.Add("url", "www.test.de/images/" + file);
ins.Add("image_data", im_data);
ins.Add("image_size", im_data.Length);
//image will be written to database
im.Dispose();
im = null;
im_data = null;
//With these initializations there shouldn't be thrown an exception
} // end for
What am I missing? With resetting the Image object and byte array, there shouldn't be another reference to the image file.
I had a look on this
IOException: The process cannot access the file 'file path' because it is being used by another process
but I couldn't figure out, how to adept to my topic.
Yes, I could store all file names just to copy them once, but I think that's the lazy way.
Kind regards
You assign a value to the variable im two times.
One time you use im = Image.FromFile(root + "\\" + file) and the other time you use im = im.GetBetterThumbnail(1024). Could it be that this opens two handles that need to be closed?
Besides, it's better to use the using statement. Then you don't have to take care of the disposing by yourself.
For example like this:
for (i = 2; i <= ws.Dimension.End.Row; i++)
{
ins = new Dictionary<string, object>(); //Dictionary to write data to database
file = ws.Cells[i, 4].Text;
System.IO.File.Copy(importFolder + "\\" + file, root + "\\" + file, true);
using (im = Image.FromFile(root + "\\" + file))
{
// I guess that this method creates its own handle
// and therefore also needs to be disposed.
using (thumbnail = im.GetBetterThumbnail(1024))
{
byte[] im_data = thumbnail.GetJpgByteArray(85);
ins.Add("url", "www.test.de/images/" + file);
ins.Add("image_data", im_data);
ins.Add("image_size", im_data.Length);
//image will be written to database
}
}
} // end for
I got the issue solved by using a stream. The memory management works really better now.
New code:
//im = Image.FromFile(root + "\\" + file);
im = Image.FromStream(File.Open(root + "\\" + file, FileMode.Open));
So could it be that this is another 'Microsoft feature'?
I have a code to create new folder and move file to that folder using FileSystemWatcher.But it gives following error.
System.IO.IOException: The process cannot access the file because it
is being us ed by another process. at
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError() at System.IO.File.Move(String
sourceFileName, String destFileName) at
FolderWatcher.Program.ProcessRenewalFolder(Object sender,
FileSystemEventA rgs e)
Following is the code
'private static void ProcessRenewalFolder(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Renewal Received.... ");
DirectoryInfo d = new DirectoryInfo(#"E:\SCN_DOCS\RENEWAL\");
DirectoryInfo dest = new DirectoryInfo(#"E:\QUEUED_SCN_DOCS\RENEWAL\");
if (!d.Exists)
{
return;
}
FileInfo[] Files = d.GetFiles("*.pdf");
string jobNo = "";
string branchCode = "";
foreach (FileInfo file in Files)
{
jobNo = file.Name;
DirectoryInfo newDir = null;
if (!Directory.Exists(dest.FullName + jobNo.ToUpper()))
{
System.IO.Directory.CreateDirectory(dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
}
Console.WriteLine(jobNo + " - " + branchCode);
try
{
File.Move(file.FullName, dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper() + "\\" + file.Name.ToUpper());
UpdateRenewal(jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}'
Please let me know the reason for this...
in below folder path
DirectoryInfo d = new DirectoryInfo(#"E:\SCN_DOCS\RENEWAL\")
any new file created using your c# application if yes than you need to dispose the object of newly created file for example
Bitmap bitmap = new Bitmap();
// your Image file creation code...
bitmap.Dispose();
as per your question you are working with .pdf file so if you created any new pdf file in above folder just before moving file, than you need to dispose that object of newly created pdf file.
see this answer https://stackoverflow.com/a/31830176/4988990
It can happen if the file is still open in the program that generated the PDF (i.e. the program that put it in the folder).
When you work with a directory watcher I suggest that you:
Put all watched files in some sort of a list.
Use a Timer and process files that have been in the list for X seconds.
If the file can't be accessed, put it in the end of the list.
In that way you get a more forgiving solution and it's not likely that you'll miss files due to errors.
Hello I tried looking through questions that may have my answer but none of them did.
I am doing web automation testing and trying to take a screenshot of when an error occurs and save it to a file in another folder.
My program has a 30 second timeout when searching for elements on the page. If after 30 seconds no element is found, it takes a SS, reloads the page, and tries again.
The first screenshot works fine. But on the second run through, it tries to save another screenshot to the folder and I get this incredibly vague error that is seemingly caused by a hundred different things, so I'm not quite sure what the problem is.
Here's my code:
public void takeScreenShot()
{
string ssPath = _persistencePath += "\\Errors";
string currTime = DateTime.Now.ToString(#"MMM-ddd-d-HH.mm");
Screenshot ss = ((ITakesScreenshot)_driver).GetScreenshot();
try
{
ss.SaveAsFile(ssPath + "\\ERROR-" + currTime + ".png", System.Drawing.Imaging.ImageFormat.Png);
ssCount = 0;
}
catch (System.Runtime.InteropServices.ExternalException)
{
ssCount++;
//error occurs here
ss.SaveAsFile(ssPath + "\\ERROR-" + currTime + "(" + ssCount + ")" + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
I initially thought the issue was that it was trying to save a file of the same name, because if the error happens during the same minute then the file name is the same. So that's why I added that catch block, in an attempt to change the name if it occurs in the same minute. But that didn't fix it.
Again I tried searching all over and couldn't find an answer. Any help is greatly appreciated.
Well, if anyone's curious I solved it. Turns out I'm just an idiot.
string ssPath = _persistencePath += "\\Errors";
this line was appending another \Errors to the target path on the second run though. thus invalidating the path, because \Errors\Errors didn't exist.
Thanks to everyone who commented/tried to help!
The Screenshot class doesn't dispose of the image resource properly, so it may be a bug in the Selenium framework. A work-around is to do it yourself:
public void takeScreenShot()
{
string ssPath = System.IO.Path.Combine(_persistencePath, #"\Errors");
string currTime = DateTime.Now.ToString(#"MMM-ddd-d-HH.mm");
string fileName = System.IO.Path.Combine(ssPath, #"\ERROR-" + currTime + ".png");
Screenshot ss = ((ITakesScreenshot)_driver).GetScreenshot();
using (MemoryStream ms = new MemoryStream(ss.AsByteArray))
using (Image screenShotImage = Image.FromStream(ms))
{
Bitmap cp = new Bitmap(screenShotImage);
cp.Save(filename, ImageFormat.Png);
cp.Dispose();
}
}
No assurance that this will fix it, but at least you can be reasonable assured that the resources are disposed of properly.
Note requires you to reference System.Drawing.dll in the project.
Edit another workaround posted.
Finally i did it!
check out this, just write completed PATHNAME,
pathname+filename+extension: #"C:\folderWithPermission\test.bmp"
JUST TAKE FOLDER WITH WRITE PERMISSIONS, was like o.0
here's the method
public DriverExecutor CaptureScreen(string filename)
{
Screenshot shot = this.myRemoteWebDriver.GetScreenshot();
MemoryStream ms;
Image imgShot;
Bitmap bitmap;
try
{
using (ms = new MemoryStream(shot.AsByteArray))
using (imgShot = Image.FromStream(ms))
using (bitmap = new Bitmap(imgShot))
{
bitmap.Save(filename, ImageFormat.Bmp);
}
}catch(Exception err){}
return this;
}
NOTE: im asssuming what remoteDriver was rightly instanced and encapsulated on a own object , i hope this help you all ^^
i'm trying to load image from local disk, and it's working. But my problem is that i'd like to check if image is available in folder, and if not - then MessageBox.Show("No image!");
Loading image:
Bitmap bitmap1 = new Bitmap(#"Documentation\\Pictures\\"+table[8]+".jpg");
pictureBox.Image=bitmap1;
You could use the File.Exists method to check whether a given file exists:
var file = Path.ChangeExtension(table[8], ".jpg");
var fullPath = Path.Combine(#"Documentation\Pictures", file);
if (!File.Exists(fullPath))
{
MessageBox.Show("No image!");
}
else
{
pictureBox.Image = new Bitmap(fullPath);
}
Try using the File.Exists method for testing if the file itself exists.
Note, however, that between invoking that method and invoking the method which actually loads the file, the file could already be gone. Thus, exception handling should be used nonetheless.
See this link for further info.
Try this
string fileName = string.Format(#"Documentation\\Pictures\\{0}.jpg",table[8]);
if(!File.Exists(fileName))
{
MessageBox.Show("No Image");
}
else
{
Picture1.Image = Image.FromFile(fileName);
}
Why would the code below throw a io.system.directorynotfound exception? I can't recreate the problem myself but another user of my code does see it, any ideas why?
Thanks
try
{
//create path
string strAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\MyApp\\Data\\logs";
//check path exists
if (!System.IO.File.Exists(strAppData))
{
System.IO.Directory.CreateDirectory(strAppData);
}
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strAppData);
int count = dir.GetFiles().Length;
if (count > 100)
{
string[] files = System.IO.Directory.GetFiles(strAppData);
foreach (string file in files)
{
System.IO.File.Delete(file);
}
}
this.fileName = fileName;
// delete the file if it exists
if (File.Exists(fileName))
{
//delete the file
File.Delete(fileName);
}
// write the data to the file
fs = File.OpenWrite(fileName);
sWriter = new StreamWriter(fs);
sWriter.WriteLine(headerText);
sWriter.Flush();
sWriter.Close();
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
Have you tried using System.IO.Directory.Exists rather than System.IO.File.Exists when checking to see if the path exists?
You're checking for the existence of a directory using System.IO.File rather than System.IO.Directory. It probably works on your machine because that directory already exists, and so the check doesn't matter.
Either way, you need to remember that the file system is volatile. Rather than checking existence, try to open the resource and handle the exception when it fails.
Check that the directory exists, not the file...
Although you're checking it, and creating it if it doesn't exist. You don't know if they have privelages to create the directory. So your Directory.CreateDirectory call may well be failing too and then sub-sequently the rest of the code will fail
http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
"Remarks
The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Existsl returns false. "
That is your error right there. Your validation does not ensure that the path to the file exists