Click here to Skip to main content
15,886,091 members
Home / Discussions / C#
   

C#

 
AnswerRe: Could use some help please. Pin
Kubajzz5-Sep-10 22:21
Kubajzz5-Sep-10 22:21 
GeneralRe: Could use some help please. Pin
pestman6-Sep-10 6:27
pestman6-Sep-10 6:27 
GeneralRe: Could use some help please. Pin
Kubajzz6-Sep-10 9:51
Kubajzz6-Sep-10 9:51 
Question"Cannot connect back" - Visual Studio 2008 Remote Debugger for C# apps Pin
Maxwell Chen5-Sep-10 20:19
Maxwell Chen5-Sep-10 20:19 
QuestionQuestion about RegEx Class Pin
Mazdak5-Sep-10 16:24
Mazdak5-Sep-10 16:24 
Answercross-post Pin
Luc Pattyn5-Sep-10 16:54
sitebuilderLuc Pattyn5-Sep-10 16:54 
AnswerRe: Question about RegEx Class [modified] Pin
PIEBALDconsult5-Sep-10 17:34
mvePIEBALDconsult5-Sep-10 17:34 
QuestionNot able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 2:39
Aseem Sharma5-Sep-10 2: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 3:04
mentorNot Active5-Sep-10 3:04 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 4:19
Aseem Sharma5-Sep-10 4:19 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Not Active5-Sep-10 5:12
mentorNot Active5-Sep-10 5:12 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 5:35
Aseem Sharma5-Sep-10 5:35 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Not Active5-Sep-10 5:56
mentorNot Active5-Sep-10 5:56 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 6:20
Aseem Sharma5-Sep-10 6:20 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Dave Kreskowiak5-Sep-10 11:09
mveDave Kreskowiak5-Sep-10 11:09 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 19:59
Aseem Sharma5-Sep-10 19:59 
AnswerRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Luc Pattyn5-Sep-10 6:26
sitebuilderLuc Pattyn5-Sep-10 6:26 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 6:39
Aseem Sharma5-Sep-10 6:39 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Dave Kreskowiak5-Sep-10 11:11
mveDave Kreskowiak5-Sep-10 11:11 
AnswerRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Pete O'Hanlon5-Sep-10 7:29
mvePete O'Hanlon5-Sep-10 7:29 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 20:02
Aseem Sharma5-Sep-10 20:02 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
NarVish6-Sep-10 0:04
NarVish6-Sep-10 0:04 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Pete O'Hanlon6-Sep-10 0:33
mvePete O'Hanlon6-Sep-10 0:33 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
NarVish6-Sep-10 1:39
NarVish6-Sep-10 1:39 
QuestionProblem : Need to make COM InterOp at runtime using reflections Passing Pointers as parameters? Pin
glitteringsound4-Sep-10 20:23
glitteringsound4-Sep-10 20: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.