File i/o question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • > Adrian

    File i/o question

    Is there a simple way to find out
    (1.) when a file is in use,
    (2.) and when any file in a directory is in use,
    so file access by another user can be blocked?

    I am asking this because I have to change a
    single user app into a multi-user app, while using
    FileShare parameters isn't possible due to the
    way the app was written.

    Adrian.


  • DeveloperX

    #2
    Re: File i/o question

    Try and open it exclusively if it fails it is in use.

    try
    {
    using (FileStream i = File.Open(filen ame, FileMode.Open,
    FileAccess.Read , FileShare.None) )
    {
    return true;
    }
    }
    catch (IOException)
    {
    return false;
    }



    Adrian < wrote:
    Is there a simple way to find out
    (1.) when a file is in use,
    (2.) and when any file in a directory is in use,
    so file access by another user can be blocked?
    >
    I am asking this because I have to change a
    single user app into a multi-user app, while using
    FileShare parameters isn't possible due to the
    way the app was written.
    >
    Adrian.

    Comment

    • Michael Nemtsev

      #3
      RE: File i/o question

      Is there a simple way to find out
      (1.) when a file is in use,
      Just try to open for writing and check if exception was thrown
      (2.) and when any file in a directory is in use,
      so file access by another user can be blocked?
      Exception will be arised

      --
      WBR,
      Michael Nemtsev :: blog: http://spaces.live.com/laflour

      "At times one remains faithful to a cause only because its opponents do not
      cease to be insipid." (c) Friedrich Nietzsche

      Comment

      • > Adrian

        #4
        Re: File i/o question


        "Michael Nemtsev" <nemtsev@msn.co mwrote in message
        news:C0F4673A-8499-49C1-981F-5C54415F90D2@mi crosoft.com...
        Is there a simple way to find out
        (1.) when a file is in use,
        >
        Just try to open for writing and check if exception was thrown
        >
        (2.) and when any file in a directory is in use,
        so file access by another user can be blocked?
        >
        Exception will be arised
        >
        --
        WBR,
        Michael Nemtsev :: blog: http://spaces.live.com/laflour
        >
        "At times one remains faithful to a cause only because its opponents do
        not
        cease to be insipid." (c) Friedrich Nietzsche
        The problem is that in one directory there are data files and files that
        contain all sorts of parameters, that are used by all sorts of shared
        methods. So just using a FileShare parameter of the data files per
        se won't do. The only thing I can think of conceptually, is ascertaining
        whether the directory is in use or not, and blocking use of the data
        files of it is. However, I am unable to conceive a method for doing that.

        Adrian.



        Comment

        • > Adrian

          #5
          Re: File i/o question


          "DeveloperX " <nntpDev@operam ail.comwrote in message
          news:1163596738 .792352.321800@ i42g2000cwa.goo glegroups.com.. .
          Try and open it exclusively if it fails it is in use.
          >
          try
          {
          using (FileStream i = File.Open(filen ame, FileMode.Open,
          FileAccess.Read , FileShare.None) )
          {
          return true;
          }
          }
          catch (IOException)
          {
          return false;
          }
          >
          >
          >
          >
          Adrian < wrote:
          Is there a simple way to find out
          (1.) when a file is in use,
          (2.) and when any file in a directory is in use,
          so file access by another user can be blocked?

          I am asking this because I have to change a
          single user app into a multi-user app, while using
          FileShare parameters isn't possible due to the
          way the app was written.

          Adrian.
          >
          The problem is that in one directory there are data files and files that
          contain all sorts of parameters, that are used by all sorts of shared
          methods. So just using a FileShare parameter of the data files per
          se won't do. The only thing I can think of conceptually, is ascertaining
          whether the directory is in use or not, and blocking use of the data
          files of it is. However, I am unable to conceive a method for doing that.

          Adrian.


          Comment

          • Willy Denoyette [MVP]

            #6
            Re: File i/o question


            "Adrian <" <not@all.access iblewrote in message
            news:455b1ebe$0 $737$5fc3050@dr eader2.news.tis cali.nl...
            |
            | "Michael Nemtsev" <nemtsev@msn.co mwrote in message
            | news:C0F4673A-8499-49C1-981F-5C54415F90D2@mi crosoft.com...
            | Is there a simple way to find out
            | (1.) when a file is in use,
            | >
            | Just try to open for writing and check if exception was thrown
            | >
            | (2.) and when any file in a directory is in use,
            | so file access by another user can be blocked?
            | >
            | Exception will be arised
            | >
            | --
            | WBR,
            | Michael Nemtsev :: blog: http://spaces.live.com/laflour
            | >
            | "At times one remains faithful to a cause only because its opponents do
            | not
            | cease to be insipid." (c) Friedrich Nietzsche
            |
            | The problem is that in one directory there are data files and files that
            | contain all sorts of parameters, that are used by all sorts of shared
            | methods. So just using a FileShare parameter of the data files per
            | se won't do. The only thing I can think of conceptually, is ascertaining
            | whether the directory is in use or not, and blocking use of the data
            | files of it is. However, I am unable to conceive a method for doing that.

            There is no such thing like "Directory in use". If you want shared access to
            files in an application, you have to open the files for shared read or
            read/write access. For files shared for read-only access, there is nothing
            special to be done, for files opened in R/W access mode you need to
            coordinate the write accesses across the process boundaries, one way to do
            this is by using a global mutex. If you wan't finer grained write access
            control, you will have to lock file regions using FileStream.Lock/Unlock.

            Willy.


            Comment

            • > Adrian

              #7
              Re: File i/o question


              "Willy Denoyette [MVP]" <willy.denoyett e@telenet.bewro te in message
              news:%23Hv3vZMC HHA.5064@TK2MSF TNGP02.phx.gbl. ..
              >
              "Adrian <" <not@all.access iblewrote in message
              news:455b1ebe$0 $737$5fc3050@dr eader2.news.tis cali.nl...
              |
              | "Michael Nemtsev" <nemtsev@msn.co mwrote in message
              | news:C0F4673A-8499-49C1-981F-5C54415F90D2@mi crosoft.com...
              | Is there a simple way to find out
              | (1.) when a file is in use,
              | >
              | Just try to open for writing and check if exception was thrown
              | >
              | (2.) and when any file in a directory is in use,
              | so file access by another user can be blocked?
              | >
              | Exception will be arised
              | >
              | --
              | WBR,
              | Michael Nemtsev :: blog: http://spaces.live.com/laflour
              | >
              | "At times one remains faithful to a cause only because its opponents
              do
              | not
              | cease to be insipid." (c) Friedrich Nietzsche
              |
              | The problem is that in one directory there are data files and files that
              | contain all sorts of parameters, that are used by all sorts of shared
              | methods. So just using a FileShare parameter of the data files per
              | se won't do. The only thing I can think of conceptually, is ascertaining
              | whether the directory is in use or not, and blocking use of the data
              | files of it is. However, I am unable to conceive a method for doing
              that.
              >
              There is no such thing like "Directory in use". If you want shared access
              to
              files in an application, you have to open the files for shared read or
              read/write access. For files shared for read-only access, there is nothing
              special to be done, for files opened in R/W access mode you need to
              coordinate the write accesses across the process boundaries, one way to do
              this is by using a global mutex. If you wan't finer grained write access
              control, you will have to lock file regions using FileStream.Lock/Unlock.
              >
              Willy.
              >
              *************** ********
              Thank you,
              Adrian.


              Comment

              • > Adrian

                #8
                Re: File i/o question

                Can't I simply put

                After a filestream fs has been opened for read:
                while(!fs.CanRe ad){}

                After a filestream fs has been opened for write:
                while(!fs.CanWr ite){}

                Adrian


                Comment

                Working...