Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Not able to delete original jpeg file after it is copied with System.IO.File.Copy()

Not able to delete original jpeg file after it is copied with System.IO.File.Copy()

Scheduled Pinned Locked Moved C#
csharphelpquestion
17 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Aseem Sharma

    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

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #8

    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 1 Reply Last reply
    0
    • L Luc Pattyn

      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 Offline
      A Offline
      Aseem Sharma
      wrote on last edited by
      #9

      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.

      D 1 Reply Last reply
      0
      • A Aseem Sharma

        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

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #10

        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

        My blog | My articles | MoXAML PowerToys | Onyx

        A N 2 Replies Last reply
        0
        • A Aseem Sharma

          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.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #11

          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

          A 1 Reply Last reply
          0
          • A Aseem Sharma

            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.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #12

            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

            1 Reply Last reply
            0
            • D 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 Kreskowiak

              A Offline
              A Offline
              Aseem Sharma
              wrote on last edited by
              #13

              Yes, 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

              1 Reply Last reply
              0
              • P Pete OHanlon

                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

                My blog | My articles | MoXAML PowerToys | Onyx

                A Offline
                A Offline
                Aseem Sharma
                wrote on last edited by
                #14

                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

                1 Reply Last reply
                0
                • P Pete OHanlon

                  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

                  My blog | My articles | MoXAML PowerToys | Onyx

                  N Offline
                  N Offline
                  NarVish
                  wrote on last edited by
                  #15

                  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.

                  P 1 Reply Last reply
                  0
                  • N NarVish

                    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.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #16

                    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

                    My blog | My articles | MoXAML PowerToys | Onyx

                    N 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      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

                      My blog | My articles | MoXAML PowerToys | Onyx

                      N Offline
                      N Offline
                      NarVish
                      wrote on last edited by
                      #17

                      Thank you..

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups