Skip to content

Commit

Permalink
Fixed an issue in ZipExtractor causing it to throw an error if file a…
Browse files Browse the repository at this point in the history
…lready exists.
  • Loading branch information
ravibpatel committed Aug 26, 2020
1 parent 2a2af4e commit 974a876
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
7 changes: 5 additions & 2 deletions AutoUpdater.NET/AutoUpdater.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
<FileVersion>1.6.1.0</FileVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>AutoUpdater.NET.snk</AssemblyOriginatorKeyFile>
<DebugType>embedded</DebugType>
<DebugSymbols>true</DebugSymbols>
<NeutralLanguage>en</NeutralLanguage>
<PackageId>Autoupdater.NET.Official</PackageId>
<IncludeSymbols>true</IncludeSymbols>
Expand All @@ -28,9 +26,14 @@
<PackageTags>autoupdate updater c# vb wpf winforms</PackageTags>
<PackageReleaseNotes>https://github.com/ravibpatel/AutoUpdater.NET/releases</PackageReleaseNotes>
<PackageOutputPath>build</PackageOutputPath>
<DocumentationFile>$(OutputPath)\$(Configuration)\AutoUpdater.NET.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>build\lib</OutputPath>
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' != 'net20' ">
<DefineConstants>NETWPF</DefineConstants>
Expand Down
61 changes: 30 additions & 31 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,48 +506,47 @@ private static void ShowError(Exception exception)
}

/// <summary>
/// Detects and exits all instances of running assembly, including current.
/// Detects and exits all instances of running assembly, including current.
/// </summary>
private static void Exit()
{
if (ApplicationExitEvent != null)
var currentProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
{
ApplicationExitEvent();
}
else
{
var currentProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
string processPath;
try
{
string processPath;
try
{
processPath = process.MainModule?.FileName;
}
catch (Win32Exception)
processPath = process.MainModule?.FileName;
}
catch (Win32Exception)
{
// Current process should be same as processes created by other instances of the application so it should be able to access modules of other instances.
// This means this is not the process we are looking for so we can safely skip this.
continue;
}

//get all instances of assembly except current
if (process.Id != currentProcess.Id && currentProcess.MainModule?.FileName == processPath)
{
if (process.CloseMainWindow())
{
// Current process should be same as processes created by other instances of the application so it should be able to access modules of other instances.
// This means this is not the process we are looking for so we can safely skip this.
continue;
process.WaitForExit((int) TimeSpan.FromSeconds(10)
.TotalMilliseconds); //give some time to process message
}

if (process.Id != currentProcess.Id && !string.IsNullOrEmpty(processPath) &&
currentProcess.MainModule?.FileName == processPath
) //get all instances of assembly except current
if (!process.HasExited)
{
if (process.CloseMainWindow())
{
process.WaitForExit((int) TimeSpan.FromSeconds(10)
.TotalMilliseconds); //give some time to process message
}

if (!process.HasExited)
{
process.Kill(); //TODO show UI message asking user to close program himself instead of silently killing it
}
process.Kill(); //TODO show UI message asking user to close program himself instead of silently killing it
}
}

}

if (ApplicationExitEvent != null)
{
ApplicationExitEvent();
}
else
{
if (_isWinFormsApplication)
{
MethodInvoker methodInvoker = Application.Exit;
Expand Down
1 change: 1 addition & 0 deletions AutoUpdater.NET/NetworkAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public NetworkAuthentication(string username, string password)
Password = password;
}

/// <inheritdoc />
public void Apply(ref MyWebClient webClient)
{
webClient.Credentials = new NetworkCredential(Username, Password);
Expand Down
Binary file modified AutoUpdater.NET/Resources/ZipExtractor.exe
Binary file not shown.
6 changes: 4 additions & 2 deletions ZipExtractor/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private void FormMain_Shown(object sender, EventArgs e)
try
{
int progress = 0;
for (var index = 0; index < entries.Count; index++)
{
if (_backgroundWorker.CancellationPending)
Expand All @@ -110,14 +111,15 @@ private void FormMain_Shown(object sender, EventArgs e)
#else
string currentFile = string.Format(Resources.CurrentFileExtracting, entry.FilenameInZip);
#endif
_backgroundWorker.ReportProgress(progress, currentFile);
int retries = 0;
bool notCopied = true;
while (notCopied)
{
try
{
#if NET45
entry.ExtractToFile(Path.Combine(path, entry.FullName));
entry.ExtractToFile(Path.Combine(path, entry.FullName), true);
#else
zip.ExtractFile(entry, Path.Combine(path, entry.FilenameInZip));
#endif
Expand All @@ -144,7 +146,7 @@ private void FormMain_Shown(object sender, EventArgs e)
}
}
int progress = (index + 1) * 100 / entries.Count;
progress = (index + 1) * 100 / entries.Count;
_backgroundWorker.ReportProgress(progress, currentFile);
_logBuilder.AppendLine($"{currentFile} [{progress}%]");
Expand Down
1 change: 0 additions & 1 deletion ZipExtractor/ZipExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<ApplicationIcon>ZipExtractor.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<SignAssembly>true</SignAssembly>
<OutputPath>..\AutoUpdater.NET\Resources</OutputPath>
<AssemblyOriginatorKeyFile>ZipExtractor.snk</AssemblyOriginatorKeyFile>
<NeutralLanguage>en</NeutralLanguage>
<DebugType>none</DebugType>
Expand Down

0 comments on commit 974a876

Please sign in to comment.