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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Windows Service in C#.Net 2.0, moving files from directory

Windows Service in C#.Net 2.0, moving files from directory

Scheduled Pinned Locked Moved C#
csharphelpcom
4 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
    adnanrafiq
    wrote on last edited by
    #1

    Hi, I tried to develop a windows service in C#.Net 2.0 to move files from a directory to another directory but my problem is this, when the file is uploaded that directory, event firs up, even the file is not completely uploaded, and it gives me error, as file is still uploading, is there any way to solve this problem other than while loop to check that file is completed uploaded and is acceable now, please suggest.

    Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com

    G 1 Reply Last reply
    0
    • A adnanrafiq

      Hi, I tried to develop a windows service in C#.Net 2.0 to move files from a directory to another directory but my problem is this, when the file is uploaded that directory, event firs up, even the file is not completely uploaded, and it gives me error, as file is still uploading, is there any way to solve this problem other than while loop to check that file is completed uploaded and is acceable now, please suggest.

      Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com

      G Offline
      G Offline
      gus_br
      wrote on last edited by
      #2

      Hi Adnan, Are you using a FileSystemWatcher to monitor the upload directory? If so, you have to make a do/while loop and try do access the file in a try/catch block inside the do/while loop. If you can access the file (it doesn't throw an exception of being current in use) you set a flag in order to exit from the loop. I think it should look something like this: bool uploadCompleted = false; do { try { File.Open("yourFileName", FileMode.Open, FileAccess.Read).Close(); uploadCompleted = true; } catch { } } while (uploadCompleted != true) I done it before and it worked. I couldn't find another approach so solve it (even made some posts in CP to find an answer). The problem is that the event is fired up even when the copy/move/creation is not complete, so you can't access the file yet. :/ Someone developed a customized code do solve this and wrote an article about this wrapper here in CP, but I thought it was too complicated and done the almost same thing I'm saying after all. Hope it helps you... CYA

      A 1 Reply Last reply
      0
      • G gus_br

        Hi Adnan, Are you using a FileSystemWatcher to monitor the upload directory? If so, you have to make a do/while loop and try do access the file in a try/catch block inside the do/while loop. If you can access the file (it doesn't throw an exception of being current in use) you set a flag in order to exit from the loop. I think it should look something like this: bool uploadCompleted = false; do { try { File.Open("yourFileName", FileMode.Open, FileAccess.Read).Close(); uploadCompleted = true; } catch { } } while (uploadCompleted != true) I done it before and it worked. I couldn't find another approach so solve it (even made some posts in CP to find an answer). The problem is that the event is fired up even when the copy/move/creation is not complete, so you can't access the file yet. :/ Someone developed a customized code do solve this and wrote an article about this wrapper here in CP, but I thought it was too complicated and done the almost same thing I'm saying after all. Hope it helps you... CYA

        A Offline
        A Offline
        adnanrafiq
        wrote on last edited by
        #3

        Hi, Thanks for replying and understanding problem Please tell me one thing, if i implement this solution of do while loop, and the uploading directory is getting files every second what will happen in that case, i think its performance will be stuck out, as other thread have to wait to finish out the while loop, or i mean wt will happened in that case. using filesystemwatcher the only .net component available to watch a directory.............. Please i hope you will understand what i m trying to say.

        Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com

        G 1 Reply Last reply
        0
        • A adnanrafiq

          Hi, Thanks for replying and understanding problem Please tell me one thing, if i implement this solution of do while loop, and the uploading directory is getting files every second what will happen in that case, i think its performance will be stuck out, as other thread have to wait to finish out the while loop, or i mean wt will happened in that case. using filesystemwatcher the only .net component available to watch a directory.............. Please i hope you will understand what i m trying to say.

          Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com

          G Offline
          G Offline
          gus_br
          wrote on last edited by
          #4

          Sure, performance in this case will be stuck. I think you can do a Thread.Sleep() inside the do loop, like 500ms. It should help a little. I don`t know another aproach. Hope someone help you more than I did. :] Cheers

          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