Solved

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

Posted on 2010-11-18
3
407 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now