Solved

extracting a text fle from network folder C# IO how do I check if file is ready

Posted on 2010-11-18
3
411 Views
Last Modified: 2012-05-10
Visual studio 2008 - C# - on windows.
H I ned to watch a network folder (Source Folder) for a text file to land there. Once I see the file I need to move that file to somewhere else (Destination).
However the 3Rd party system that writes the file to Source may still be sriting contents or may not even be finished copying the file to Source when I attempt to move it to destination.
I want to wait till the 3RdPart app is finished with the file , then move it.
What is the coding I should use ? and what classes and methods can I use please?
Please paste some simple code lines rather than directing me to links if possible.
Thanks.
0
Comment
Question by:indikad
3 Comments
 
LVL 3

Accepted Solution

by:
saifikram_md earned 400 total points
ID: 34169363
FileSystemWatcher is the best way to use. follow the link below

FileWatcher
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 100 total points
ID: 34170250
Try this:
If Len(strFileName) > 0 AndAlso System.IO.File.Exists(strFileName) Then
            'need to make sure that the file is ready to be copied.
            Do While True
                'try to open the file for read. If failed, try again, until it's ready
                Dim tmp As System.IO.StreamReader = System.IO.File.OpenText(strFileName)

                If tmp IsNot Nothing Then
                    tmp.Close()
                    tmp.Dispose()
                    System.Threading.Thread.Sleep(10)
                    Exit Do
                End If
                System.Threading.Thread.Sleep(10)
            Loop
        End If

Open in new window

0
 

Author Closing Comment

by:indikad
ID: 34185696
Thanks very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question