J'ai besoin de télécharger un gros fichier par programme avant de le traiter. Quelle est la meilleure façon de faire cela? Comme le fichier est volumineux, je souhaite un délai spécifique pour que je puisse sortir de force.Comment télécharger un gros fichier par programmation en C#
Je connais WebClient.DownloadFile(). Mais il ne semble pas y avoir de délai spécifique pour attendre la sortie forcée.
try
{
WebClient client = new WebClient();
Uri uri = new Uri(inputFileUrl);
client.DownloadFile(uri, outputFile);
}
catch (Exception ex)
{
throw;
}
Une autre façon consiste à utiliser une ligne de commande utilitaire (wget) pour télécharger le fichier et le feu de la commande en utilisant ProcessStartInfo et le processus d'utilisation WaitForExit (int ms) de sortie de force.
ProcessStartInfo startInfo = new ProcessStartInfo();
//set startInfo object
try
{
using (Process exeProcess = Process.Start(startInfo))
{
//wait for time specified
exeProcess.WaitForExit(1000 * 60 * 60);//wait till 1m
//check if process has exited
if (!exeProcess.HasExited)
{
//kill process and throw ex
exeProcess.Kill();
throw new ApplicationException("Downloading timed out");
}
}
}
catch (Exception ex)
{
throw;
}
Y a-t-il un meilleur moyen? S'il vous plaît aider. Merci.
Quelle est la uri ? est-ce ftp? – t0mm13b