简体   繁体   中英

Check/understand if a file has been completely downloaded (using c#)

I wanted to develop a C# application (client side) that monitors a folder (every X seconds) in which another web application downloads files (typically Office files, PDF, ...). Once the file is completely downloaded, the client application must open the document and perform other tasks/operations.

Is there any way to check/understand when a file has been FULLY downloaded (not knowing its actual size)?

Best

Stefano

You can use a static Timer and start it from the Application_Start() method in Global.asax . Within Global.asax , add the following field:

static Timer _timer = null;

Then you can make your Application_Start() like:

void Application_Start(object sender, EventArgs e)
{
    if (_timer == null)
    {
        _timer = new Timer();
        _timer.Interval = 1000; // some interval
        _timer.Elapsed += new ElapsedEventHandler;
        _timer.Start();
    }
}

void ElapsedEventHandler(object sender, ElapsedEventArgs e)
{
     string filePath = "/files/myfile.txt";
     if (Directory.Exists(filePath))
     {
         other_tasks_operations();
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM