Not able to delete original jpeg file after it is copied with System.IO.File.Copy()
-
Hi all, I am doing a very simple operation via c#. 1 I just copy an existing JPEG file in temp directory. 2 Then I open the copied Jpeg file from temp directory. 3 I try to delete the original Jpeg file and application crashes. Please see below c# function for this:
using System.IO;
public void DeleteAfterCopyingOriginalImageFile(string strOriginalJpeg)
{
// Create "JpegTemp" sub-folder at temporary directory path
string strTempFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "JpegTemp");
if (!Directory.Exists(strTempFolder))
{
DirectoryInfo directory = Directory.CreateDirectory(strTempFolder);
directory.Attributes = directory.Attributes | FileAttributes.Hidden;
}// Get the temp file path
string strTempJpeg = System.IO.Path.Combine(strTempFolder, "TempImage.jpg");// Copy the original JPEG as below
if (!File.Exists(strTempJpeg))
{
File.Copy(strOriginalJpeg, strTempJpeg);
}// Open the "TempImage.jpg" file in default viewer. Doing this lock the original jpeg file.
// Don't know why? It is creating problem for me.
Process.Start(strTempJpeg);// Delete original JPEG file. Since the file is locked in above step, application throws exception
// "File being used by some other process" and then application crashes.
File.Delete(strOriginalJpeg);
}I need to follow exactly these steps (cannot delete original file before opening TempImage.jpg). Can anybody help in knowing why copy and opening of temp file lock the original file. How can I prevent original jpeg file from being locked. I am new in .Net. Please help me. More Info about the user case: The user case is that user cannot perform any operation on original file. So a temporary copy of original file is created in temp folder. User can then open/edit temp file. And if he thinks that he can replace original file with that of edited (copied in temp directory) one or he does not need original file any more, he can delete original file (and if required, can make temp file as the original one). So I strictly need to follow the same steps. Regards Aseem Thanks in Advance
modified on Sunday, September 5, 2010 10:18 AM
A few suggestions: 1. put all of it inside a try-catch and show the entire Exception.ToString(). 2. is the original file path a fully qualified path? is there any chance both paths get resolved to the same file? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
A few suggestions: 1. put all of it inside a try-catch and show the entire Exception.ToString(). 2. is the original file path a fully qualified path? is there any chance both paths get resolved to the same file? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
1 Here is the Exception details "System.IO.IOException: The process cannot access the file 'MyPhotograph.jpg' because it is being used by another process.\r\n at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)\r\n at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)\r\n at System.IO.Directory.Delete(String path, Boolean recursive)\r\n at NG.DataRep.CleanImage(String strFile) in E:\\Aseem Sharma\\KB\\My Software Development\\NG\\DataRep.cs:line 153" 2 Yes, I have confirmed, original file path is a fully qualified path and both temp file and original file are different file path.
-
Hi all, I am doing a very simple operation via c#. 1 I just copy an existing JPEG file in temp directory. 2 Then I open the copied Jpeg file from temp directory. 3 I try to delete the original Jpeg file and application crashes. Please see below c# function for this:
using System.IO;
public void DeleteAfterCopyingOriginalImageFile(string strOriginalJpeg)
{
// Create "JpegTemp" sub-folder at temporary directory path
string strTempFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "JpegTemp");
if (!Directory.Exists(strTempFolder))
{
DirectoryInfo directory = Directory.CreateDirectory(strTempFolder);
directory.Attributes = directory.Attributes | FileAttributes.Hidden;
}// Get the temp file path
string strTempJpeg = System.IO.Path.Combine(strTempFolder, "TempImage.jpg");// Copy the original JPEG as below
if (!File.Exists(strTempJpeg))
{
File.Copy(strOriginalJpeg, strTempJpeg);
}// Open the "TempImage.jpg" file in default viewer. Doing this lock the original jpeg file.
// Don't know why? It is creating problem for me.
Process.Start(strTempJpeg);// Delete original JPEG file. Since the file is locked in above step, application throws exception
// "File being used by some other process" and then application crashes.
File.Delete(strOriginalJpeg);
}I need to follow exactly these steps (cannot delete original file before opening TempImage.jpg). Can anybody help in knowing why copy and opening of temp file lock the original file. How can I prevent original jpeg file from being locked. I am new in .Net. Please help me. More Info about the user case: The user case is that user cannot perform any operation on original file. So a temporary copy of original file is created in temp folder. User can then open/edit temp file. And if he thinks that he can replace original file with that of edited (copied in temp directory) one or he does not need original file any more, he can delete original file (and if required, can make temp file as the original one). So I strictly need to follow the same steps. Regards Aseem Thanks in Advance
modified on Sunday, September 5, 2010 10:18 AM
The original file must have a handle to it then - either you are getting this somewhere in your code, or another process is. To find out which, use ProcessExplorer and search for any handles to this file.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
One more requirement of my application is that I do not want to let the user know where the original file is present. User can just click a thumbnail from my application to open it. My application then makes a copy of original file in temp folder and open that temp copy of file. And whatever the user case is, I think I am doing something wrong in my code that deleting original file generates exception and crashing my application. Just opening (and not even editing) the temp file should have no impact on the deletion of original file.
That depends entirely on how the app is using the file in the first place. If you're doing something like getting a thumbnail of an image file, your app is probably keeping the file open for the like of the thumbnail, in which case, you won't be able to delete it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
1 Here is the Exception details "System.IO.IOException: The process cannot access the file 'MyPhotograph.jpg' because it is being used by another process.\r\n at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)\r\n at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)\r\n at System.IO.Directory.Delete(String path, Boolean recursive)\r\n at NG.DataRep.CleanImage(String strFile) in E:\\Aseem Sharma\\KB\\My Software Development\\NG\\DataRep.cs:line 153" 2 Yes, I have confirmed, original file path is a fully qualified path and both temp file and original file are different file path.
Aseem Sharma wrote:
The process cannot access the file 'MyPhotograph.jpg' because it is being used by another process.
That process is either your own app because you created and Image or Bitmap using a .FromFile method, which keeps the file open for the lifetime of the Image or Bitmap object. You have to use a different method for getting the image without locking the file open. This would probably be supplying your own FileStream object, opeing the file, then passing that to the contructor of the Image or Bitmap object, then closing the file. That process does not lock the file allowing you to do whatever you want.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
That depends entirely on how the app is using the file in the first place. If you're doing something like getting a thumbnail of an image file, your app is probably keeping the file open for the like of the thumbnail, in which case, you won't be able to delete it.
A guide to posting questions on CodeProject[^]
Dave KreskowiakYes, you are absolutely right. It was the Thumbnail who keep the original file locked. I myself started looking into some irrelevant way. Your reply made me check the Thumbnail part of code and I could fix my problem. Thanks for this. Best Regards Aseem
-
The original file must have a handle to it then - either you are getting this somewhere in your code, or another process is. To find out which, use ProcessExplorer and search for any handles to this file.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Thanks for your reply. I have fixed the problem. You were right in that my application was itself holding the file handle. Thanks for helping me. Best Regards Aseem
-
The original file must have a handle to it then - either you are getting this somewhere in your code, or another process is. To find out which, use ProcessExplorer and search for any handles to this file.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
Do you mean Process Explorer is task manager or is it separate window in visual studio editor. If it belongs to visula studio, could you let me know where can I find it. Thanks in advance.
NarVish wrote:
Do you mean Process Explorer is task manager or is it separate window in visual studio editor.
Neither. It's one of the most useful utilities you can download. You can get the latest info here[^].
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
NarVish wrote:
Do you mean Process Explorer is task manager or is it separate window in visual studio editor.
Neither. It's one of the most useful utilities you can download. You can get the latest info here[^].
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads