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. Avoid file deletion

Avoid file deletion

Scheduled Pinned Locked Moved C#
questionxml
7 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.
  • T Offline
    T Offline
    Tanuja123
    wrote on last edited by
    #1

    How can i prevent user from deleting a file when the file is under use by another process ? I have an application which creates a directory called (say) "Test" and creates a default "Text.xml" file in it.I dont want user to delete the "Test.xml" when the application is in use. what is the code for it. Currently ,user can delete the file when application is open. Please tell me the code for it...

    R P 2 Replies Last reply
    0
    • T Tanuja123

      How can i prevent user from deleting a file when the file is under use by another process ? I have an application which creates a directory called (say) "Test" and creates a default "Text.xml" file in it.I dont want user to delete the "Test.xml" when the application is in use. what is the code for it. Currently ,user can delete the file when application is open. Please tell me the code for it...

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      You can't delete an open file (well you might be able to if you fiddle around with permissions enough) so just keep the file open for the duration of your app. This code:

      [STAThread]
      static void Main(string[] args)
      {
      FileStream fs = File.Open(@"C:\bobbins.txt", FileMode.Open);
      Console.ReadLine();
      fs.Close();
      }

      Prevents me from deleting the file bobbins.txt until my console app closes.

      Regards, Rob Philpott.

      N 1 Reply Last reply
      0
      • T Tanuja123

        How can i prevent user from deleting a file when the file is under use by another process ? I have an application which creates a directory called (say) "Test" and creates a default "Text.xml" file in it.I dont want user to delete the "Test.xml" when the application is in use. what is the code for it. Currently ,user can delete the file when application is open. Please tell me the code for it...

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        snoby wrote:

        How can i prevent user from deleting a file when the file is under use by another process ?

        The file cannot be deleted while an application is using it. What is ... well part of Windows. Run an application and try to delete it ... you cant. Other wise if I am not understanding you correctly, you can open the file for reading and only after the appliation has finished with the file, you can close the file. something like ...

        // this is just a concept ...
        form onload()
        open file for reading
        form_onclose()
        close file

        Hope it helps ... Regards,


        The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

        L 1 Reply Last reply
        0
        • R Rob Philpott

          You can't delete an open file (well you might be able to if you fiddle around with permissions enough) so just keep the file open for the duration of your app. This code:

          [STAThread]
          static void Main(string[] args)
          {
          FileStream fs = File.Open(@"C:\bobbins.txt", FileMode.Open);
          Console.ReadLine();
          fs.Close();
          }

          Prevents me from deleting the file bobbins.txt until my console app closes.

          Regards, Rob Philpott.

          N Offline
          N Offline
          Nilesh Hapse
          wrote on last edited by
          #4

          Also I think there is a lock method for FileStream. Don't know it's efficiency.

          "Legacy code" often differs from its suggested alternative by actually working and scaling. —Bjarne Stroustrup

          modified on Friday, March 14, 2008 7:10 AM

          R 1 Reply Last reply
          0
          • N Nilesh Hapse

            Also I think there is a lock method for FileStream. Don't know it's efficiency.

            "Legacy code" often differs from its suggested alternative by actually working and scaling. —Bjarne Stroustrup

            modified on Friday, March 14, 2008 7:10 AM

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            Ah, not sure about that; never used it, but if so that sounds better!

            Regards, Rob Philpott.

            1 Reply Last reply
            0
            • P Programm3r

              snoby wrote:

              How can i prevent user from deleting a file when the file is under use by another process ?

              The file cannot be deleted while an application is using it. What is ... well part of Windows. Run an application and try to delete it ... you cant. Other wise if I am not understanding you correctly, you can open the file for reading and only after the appliation has finished with the file, you can close the file. something like ...

              // this is just a concept ...
              form onload()
              open file for reading
              form_onclose()
              close file

              Hope it helps ... Regards,


              The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #6

              What if want to be sure that no data is lost? Keeping a file opened could cause all data is lost on crush or immediate shut down. I suppose that such construct could be used:

              open file;
              // ...
              write data which shouldn't get lost;
              close the file to save it; (*)
              reopen it;
              // ...
              and so on.

              But in could theoretically happen that user opens a file just after (*), before it's opened in the next command. The propability of this is near 0, anyway what to do then?

              Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

              D 1 Reply Last reply
              0
              • L Lutoslaw

                What if want to be sure that no data is lost? Keeping a file opened could cause all data is lost on crush or immediate shut down. I suppose that such construct could be used:

                open file;
                // ...
                write data which shouldn't get lost;
                close the file to save it; (*)
                reopen it;
                // ...
                and so on.

                But in could theoretically happen that user opens a file just after (*), before it's opened in the next command. The propability of this is near 0, anyway what to do then?

                Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

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

                Just holding the file open won't do that. It's when you write data to the file without flushing it, or shut the machine off in the middle of a disk write that you run into this problem.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                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