Forum: PC-Programmierung WebClient, DownloadFileTaskAsync und Filehandle


von Frank L. (frank_l)


Lesenswert?

Hallo Zusammen,

ich habe mit den folgenden Code ein kleines Problem:
1
public async Task Download(string filename, DownloadProgressChangedEventHandler showDownloadProgressEvent)
2
{
3
    using (var webClient = new FtpWebClient())
4
    {
5
        var credentials = (NetworkCredential)_credentials;
6
        webClient.Credentials = credentials;
7
        if (showDownloadProgressEvent != null)
8
        {
9
            webClient.DownloadProgressChanged += showDownloadProgressEvent;
10
        }
11
        var task = webClient.DownloadFileTaskAsync(Href, filename);
12
        await task;
13
    }
14
}

Der Code lädt brav eine Datei vom Server in ein lokales Verzeichnis. 
Alles gut. Versuche ich aber anschließend auf diese Datei mit dem 
folgenden Code zuzugreifen erhalte ich eine Exception.
1
        private static string Load(string fileName)
2
        {
3
            var sb = new StringBuilder();
4
            using (var sr = new StreamReader(fileName))
5
            {
6
                string line;
7
                while ((line = sr.ReadLine()) != null)
8
                {
9
                    sb.AppendLine(line);
10
                }
11
            }
12
            return sb.ToString();
13
        }

Exception:
1
2016-07-21 12:37:28,687 DEBUG 9 Project.Applications.ViewModel.ProjectViewViewModel (null) - System.IO.IOException: The process cannot access the file 'C:\Users\frank\Documents\SP Prov Workbench\Configuration\TempDownloads\Stylestmp6195.css' because it is being used by another process.
2
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
3
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
4
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
5
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
6
   at System.IO.StreamReader..ctor(String path)
7
   at Editor.Applications.ViewModel.EditorViewViewModel.Load(String fileName) in C:\Users\frank\Documents\Visual Studio 2015\Projects\GitHub\SPProv\Workbench\Editor.Application\ViewModel\EditorViewViewModel.cs:line 119
8
   at Editor.Applications.ViewModel.EditorViewViewModel.set_PathItem(IPathItem value) in C:\Users\frank\Documents\Visual Studio 2015\Projects\GitHub\SPProv\Workbench\Editor.Application\ViewModel\EditorViewViewModel.cs:line 145
9
   at Project.Applications.ViewModel.ProjectViewViewModel.OpenScriptDocumentExecute(Object obj) in C:\Users\frank\Documents\Visual Studio 2015\Projects\GitHub\SPProv\Workbench\Project.Applications\ViewModel\ProjectViewViewModel.cs:line 432

Eindeutig ein Hinweis dafür, dass die Datei durch den Downloadprocess 
noch festgehalten wird.

Irgend jemand eine gute Idee, wie ich den Zugriff erhalte?

Thread Sleep und andere Dinge habe ich bereits versucht, bringen alle 
nichts.

Gruß
Frank

von Peter II (Gast)


Lesenswert?

Versuche mal vorher ein Dispose vom Task zu machen.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.