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. .NET (Core and Framework)
  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 .NET (Core and Framework)
csharphelpquestion
2 Posts 2 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 Offline
    A Offline
    Aseem Sharma
    wrote on last edited by
    #1

    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

    N 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

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      DO NOT cross post or repost you question. You have received responses to your original post. Posting again will not allow others to see all the context and what has been tried already. Read "How to get answers..." at the top of this forum!


      I know the language. I've read a book. - _Madmatt

      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