How to tell if a file is currently being written to...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Um9nZWxpbw==?=

    How to tell if a file is currently being written to...

    hey, got a method here (that I took from somewhere) that gets a list of all
    the files in a folder.,

    /// <summary>
    /// Method that gets all the files in a directory and returns an
    arraylist of all the files.
    /// </summary>
    /// <param name="di">Direc tory Info Object</param>
    /// <param name="searchPat tern">Search Pattern (*.pdf)</param>
    /// <param name="MyFiles"> referenced array list</param>
    private void GetFiles(Direct oryInfo di, string searchPattern, ref
    ArrayList MyFiles)
    {
    foreach (FileInfo fi in di.GetFiles(sea rchPattern))
    {
    MyFiles.Add(fi. FullName);
    }
    // comment out the following to not Search in subdirctories
    foreach (DirectoryInfo d in di.GetDirectori es())
    {
    GetFiles(d, searchPattern, ref MyFiles);
    }

    }



    the problem is sometimes people are going to be pasting massive ifles in
    this folder., like 100+ MB of size. and as you know it will put the file
    there, but it wont be done yet.

    what kind of "if" statement do I need that checks the file's status that its
    not currently being written to?

    thanks,
  • Jeroen Mostert

    #2
    Re: How to tell if a file is currently being written to...

    Rogelio wrote:
    what kind of "if" statement do I need that checks the file's status that its
    not currently being written to?
    >
    Unfortunately, there's no foolproof way of determining when a process is
    "done" writing to a file. The best you can hope for is that the process
    keeps open a write lock while it's writing to the file. Even then it mustn't
    write in chunks (opening the file, appending some data, then closing it again).

    If it does keep open a write lock, all you have to do is try to open the
    file for writing with a lock of your own (new FileStream(path ,
    FileMode.Open, FileAccess.Writ e, FileShare.None) ). If this fails, the file
    is most likely still in use.

    Another heuristic is to only process files whose last modified time
    (FileInfo.LastW riteTime) is past some specified threshold, so you're
    reasonably sure no more writes will occur.

    --
    J.

    Comment

    • =?Utf-8?B?Um9nZWxpbw==?=

      #3
      Re: How to tell if a file is currently being written to...

      ok. heres my solution....


      /// <summary>
      /// Method that gets all the files in a directory and returns an
      arraylist of all the files.
      /// </summary>
      /// <param name="di">Direc tory Info Object</param>
      /// <param name="searchPat tern">Search Pattern (*.pdf)</param>
      /// <param name="MyFiles"> referenced array list</param>
      private void GetFiles(Direct oryInfo di, string searchPattern, ref
      ArrayList MyFiles)
      {
      foreach (FileInfo fi in di.GetFiles(sea rchPattern))
      {
      //check if the last write time of the file was not 20
      seconds ago or sooner.
      //this prevents a file currently being written to to be
      added to the list of files.
      if (fi.LastWriteTi me < DateTime.Now.Ad dSeconds(-20))
      {
      MyFiles.Add(fi. FullName);
      }
      }
      // Search in subdirctories
      foreach (DirectoryInfo d in di.GetDirectori es())
      {
      GetFiles(d, searchPattern, ref MyFiles);
      }

      }

      "Jeroen Mostert" wrote:
      Rogelio wrote:
      what kind of "if" statement do I need that checks the file's status that its
      not currently being written to?
      Unfortunately, there's no foolproof way of determining when a process is
      "done" writing to a file. The best you can hope for is that the process
      keeps open a write lock while it's writing to the file. Even then it mustn't
      write in chunks (opening the file, appending some data, then closing it again).
      >
      If it does keep open a write lock, all you have to do is try to open the
      file for writing with a lock of your own (new FileStream(path ,
      FileMode.Open, FileAccess.Writ e, FileShare.None) ). If this fails, the file
      is most likely still in use.
      >
      Another heuristic is to only process files whose last modified time
      (FileInfo.LastW riteTime) is past some specified threshold, so you're
      reasonably sure no more writes will occur.
      >
      --
      J.
      >

      Comment

      • Pavel Minaev

        #4
        Re: How to tell if a file is currently being written to...

        On Aug 15, 9:17 pm, Rogelio <Roge...@discus sions.microsoft .comwrote:
        ok. heres my solution....
        >
                /// <summary>
                /// Method that gets all the files in a directory and returns an
        arraylist of all the files.
                /// </summary>
                /// <param name="di">Direc tory Info Object</param>
                /// <param name="searchPat tern">Search Pattern (*.pdf)</param>
                /// <param name="MyFiles"> referenced array list</param>
                private void GetFiles(Direct oryInfo di, string searchPattern, ref
        ArrayList MyFiles)
                {
                    foreach (FileInfo fi in di.GetFiles(sea rchPattern))
                    {
                        //check if the last write time of the file was not 20
        seconds ago or sooner.
                        //this prevents a file currently being written to to be
        added to the list of files.
                        if (fi.LastWriteTi me < DateTime.Now.Ad dSeconds(-20))
                        {
                            MyFiles.Add(fi. FullName);
                        }
                    }
                    // Search in subdirctories
                    foreach (DirectoryInfo d in di.GetDirectori es())
                    {
                        GetFiles(d, searchPattern, ref MyFiles);
                    }
        >
                }
        If you really want to follow the route of keeping track of the time of
        the last write, then why not use FileSystemWatch er class with
        NotifyFilters.L astWrite? It will be much more efficient.

        Comment

        Working...