المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Not able to delete original jpeg file after it is copied with System.IO.File.Copy()



C# Programming
09-06-2010, 01:53 AM
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