Skip to content

Commit b131a15

Browse files
Deadpiklesdijoseph
andcommitted
Fix file access issues in LocalFileDownloader
Remove unneeded .Close() calls, too. Also set FileShare to .Read just for safety. Closes #512 Co-Authored-By: sdijoseph <6656812+sdijoseph@users.noreply.github.com>
1 parent a38b203 commit b131a15

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/NetSparkle/Downloaders/LocalFileDownloader.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private async Task CopyFileAsync(string sourceFile, string destinationFile, Canc
109109
using (var sourceStream =
110110
new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, fileOptions))
111111
using (var destinationStream =
112-
new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize, fileOptions))
112+
new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read, bufferSize, fileOptions))
113113
{
114114
IsDownloading = true;
115115
var wasCanceled = false;
@@ -127,8 +127,8 @@ private async Task CopyFileAsync(string sourceFile, string destinationFile, Canc
127127
UpdateDownloadProgress(totalRead, totalFileLength);
128128
}
129129
IsDownloading = false;
130-
DownloadFileCompleted?.Invoke(this, new AsyncCompletedEventArgs(null, wasCanceled, null));
131130
}
131+
DownloadFileCompleted?.Invoke(this, new AsyncCompletedEventArgs(null, wasCanceled, null));
132132
}
133133
catch (Exception e)
134134
{

src/NetSparkle/Downloaders/WebClientFileDownloader.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private async Task StartFileDownloadAsync(Uri uri, string downloadFilePath)
161161
}
162162
return;
163163
}
164-
using (FileStream fileStream = new FileStream(downloadFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
164+
using (FileStream fileStream = new FileStream(downloadFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 8192, true))
165165
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
166166
{
167167
long totalLength = response.Content.Headers.ContentLength ?? 0;
@@ -191,11 +191,9 @@ private async Task StartFileDownloadAsync(Uri uri, string downloadFilePath)
191191
UpdateDownloadProgress(totalRead, totalLength);
192192
} while (IsDownloading);
193193
IsDownloading = false;
194-
fileStream.Close();
195-
contentStream.Close();
196-
UpdateDownloadProgress(totalRead, totalLength);
197-
DownloadFileCompleted?.Invoke(this, new AsyncCompletedEventArgs(null, false, null));
198194
}
195+
UpdateDownloadProgress(totalRead, totalLength);
196+
DownloadFileCompleted?.Invoke(this, new AsyncCompletedEventArgs(null, false, null));
199197
}
200198
}
201199
catch (Exception e)

0 commit comments

Comments
 (0)