Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

911 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

19 Experts available now in Live!

Get 1:1 Help Now