Click here to Skip to main content
15,560,045 members
Home / Discussions / C#
   

C#

 
AnswerRe: Could use some help please. Pin
Kubajzz5-Sep-10 23:21
Kubajzz5-Sep-10 23:21 
GeneralRe: Could use some help please. Pin
pestman6-Sep-10 7:27
pestman6-Sep-10 7:27 
GeneralRe: Could use some help please. Pin
Kubajzz6-Sep-10 10:51
Kubajzz6-Sep-10 10:51 
Question"Cannot connect back" - Visual Studio 2008 Remote Debugger for C# apps Pin
Maxwell Chen5-Sep-10 21:19
Maxwell Chen5-Sep-10 21:19 
QuestionQuestion about RegEx Class Pin
Mazdak5-Sep-10 17:24
Mazdak5-Sep-10 17:24 
Answercross-post Pin
Luc Pattyn5-Sep-10 17:54
sitebuilderLuc Pattyn5-Sep-10 17:54 
AnswerRe: Question about RegEx Class [modified] Pin
PIEBALDconsult5-Sep-10 18:34
professionalPIEBALDconsult5-Sep-10 18:34 
QuestionNot able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 3:39
Aseem Sharma5-Sep-10 3:39 
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

AnswerRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Not Active5-Sep-10 4:04
mentorNot Active5-Sep-10 4:04 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 5:19
Aseem Sharma5-Sep-10 5:19 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Not Active5-Sep-10 6:12
mentorNot Active5-Sep-10 6:12 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 6:35
Aseem Sharma5-Sep-10 6:35 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Not Active5-Sep-10 6:56
mentorNot Active5-Sep-10 6:56 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 7:20
Aseem Sharma5-Sep-10 7:20 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Dave Kreskowiak5-Sep-10 12:09
mveDave Kreskowiak5-Sep-10 12:09 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 20:59
Aseem Sharma5-Sep-10 20:59 
AnswerRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Luc Pattyn5-Sep-10 7:26
sitebuilderLuc Pattyn5-Sep-10 7:26 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 7:39
Aseem Sharma5-Sep-10 7:39 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Dave Kreskowiak5-Sep-10 12:11
mveDave Kreskowiak5-Sep-10 12:11 
AnswerRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Pete O'Hanlon5-Sep-10 8:29
mvaPete O'Hanlon5-Sep-10 8:29 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 21:02
Aseem Sharma5-Sep-10 21:02 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
NarVish6-Sep-10 1:04
NarVish6-Sep-10 1:04 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Pete O'Hanlon6-Sep-10 1:33
mvaPete O'Hanlon6-Sep-10 1:33 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
NarVish6-Sep-10 2:39
NarVish6-Sep-10 2:39 
QuestionProblem : Need to make COM InterOp at runtime using reflections Passing Pointers as parameters? Pin
glitteringsound4-Sep-10 21:23
glitteringsound4-Sep-10 21:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.