• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

.net detecting file in use

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    .net detecting file in use

    I have 2 processes writing some info to the same file. It's a small file the contents of which get replaced.

    I'm trying to figure out a reliable way of detecting the file is in use so I Can abort or retry or whatever.

    I create a FileStream using Mode=Create, Access=Write, Share=None

    The problem is that this will throw an IOException if the file is in use. But I can't tell that this is the problem. It could be any other FileException. The only way I could tell is to look at the exception message.

    Any suggestions? Surely there must be a simple way. Haven't turned anything by googling so maybe not.

    #2
    Originally posted by ASB View Post
    I have 2 processes writing some info to the same file. It's a small file the contents of which get replaced.

    I'm trying to figure out a reliable way of detecting the file is in use so I Can abort or retry or whatever.

    I create a FileStream using Mode=Create, Access=Write, Share=None

    The problem is that this will throw an IOException if the file is in use. But I can't tell that this is the problem. It could be any other FileException. The only way I could tell is to look at the exception message.

    Any suggestions? Surely there must be a simple way. Haven't turned anything by googling so maybe not.
    Use a separate lock file. If lock file exists don't try to open the file. If lock file doesn't exist open file and then create lock file. When file is closed remove the lock file.

    The next stop would be to check if the lock file has existed for a significant amount of time. If it has kill the other process and restart that program.

    Granted its a rather unix way of managing files but if it works steal it.
    merely at clientco for the entertainment

    Comment


      #3
      Originally posted by ASB View Post
      I have 2 processes writing some info to the same file. It's a small file the contents of which get replaced.

      I'm trying to figure out a reliable way of detecting the file is in use so I Can abort or retry or whatever.

      I create a FileStream using Mode=Create, Access=Write, Share=None

      The problem is that this will throw an IOException if the file is in use. But I can't tell that this is the problem. It could be any other FileException. The only way I could tell is to look at the exception message.

      Any suggestions? Surely there must be a simple way. Haven't turned anything by googling so maybe not.
      I assume you don't want to write code that looks at the message because it is a string and isn't all type safe etc?
      Last edited by russell; 15 March 2012, 14:36.

      Comment


        #4
        Originally posted by eek View Post
        Use a separate lock file. If lock file exists don't try to open the file. If lock file doesn't exist open file and then create lock file. When file is closed remove the lock file.

        The next stop would be to check if the lock file has existed for a significant amount of time. If it has kill the other process and restart that program.

        Granted its a rather unix way of managing files but if it works steal it.
        What if another app is using the file, and that app doesn't implement your idea of creating lock files?

        Comment


          #5
          Originally posted by russell View Post
          I assume you don't want to write code that looks at the message because it is a string and isn't all type safe etc? You could do exception.Message.Contains("in use") or something similar?
          Should work a treat, except in Germany, or France, or Japan, or...

          Comment


            #6
            c# - Is there a way to check if a file is in use? - Stack Overflow

            I think some kind of retry attempts logic mechanism is the only way to do it.
            Vote Corbyn ! Save this country !

            Comment


              #7
              Originally posted by russell
              Well I was just giving an example, not going to do his job for him.
              No you were offering a completely idiotic solution and Nick has rightly criticised it.
              Vote Corbyn ! Save this country !

              Comment


                #8
                Originally posted by fullyautomatix View Post
                No you were offering a completely idiotic solution and Nick has rightly criticised it.
                +1 fullautomatix's stackoverflow link is the right approach if you simply want to check if a file .

                If you have full control the unix lock file approach provides you with more information which may be useful if processes hit a snag.
                merely at clientco for the entertainment

                Comment


                  #9
                  Originally posted by fullyautomatix View Post
                  No you were offering a completely idiotic solution and Nick has rightly criticised it.
                  Yes it was a bit stupid. I have removed it now.

                  Comment


                    #10
                    Originally posted by ASB View Post
                    I have 2 processes writing some info to the same file.

                    Any suggestions? Surely there must be a simple way. Haven't turned anything by googling so maybe not.
                    1) Don't
                    2) Use DB

                    Comment

                    Working...
                    X