Skip to content

Commit

Permalink
A few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Genbox committed Apr 18, 2023
1 parent eac9f4b commit f1f9def
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/FileShellExtension.cs
Expand Up @@ -36,13 +36,13 @@ public static void Register(string fileType, string shellKeyName, string menuTex
// add context menu to the registry
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))
{
key?.SetValue(null, menuText);
key.SetValue(null, menuText);
}

// add command that is invoked to the registry
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format(@"{0}\command", regPath)))
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey($@"{regPath}\command"))
{
key?.SetValue(null, menuCommand);
key.SetValue(null, menuCommand);
}
}

Expand Down
51 changes: 32 additions & 19 deletions src/Program.cs
Expand Up @@ -30,8 +30,7 @@ public static async Task Main(string[] args)
}
catch (Exception e)
{
Console.WriteLine("An error happened:");
Console.WriteLine(e);
WriteError("Unknown error happened: " + e.Message);
}
}
}
Expand All @@ -58,10 +57,7 @@ private static bool ProcessCommand(string[] args)
// register the context menu
FileShellExtension.Register(FileType, KeyName, MenuText, menuCommand);

Console.WriteLine("The '{0}' shell extension was registered.", KeyName);
Console.WriteLine("Press a key to continue");
Console.ReadKey();

WriteSuccess($"The '{KeyName}' shell extension was registered.");
return true;
}

Expand All @@ -73,10 +69,7 @@ private static bool ProcessCommand(string[] args)
// unregister the context menu
FileShellExtension.Unregister(FileType, KeyName);

Console.WriteLine("The '{0}' shell extension was unregistered.", KeyName);
Console.WriteLine("Press a key to continue");
Console.ReadKey();

WriteSuccess($"The '{KeyName}' shell extension was unregistered.");
return true;
}

Expand All @@ -95,10 +88,22 @@ private static void RestartBinaryAsAdminIfRequired()
if (UacHelper.IsProcessElevated)
return;

Console.WriteLine("You have to run as admin to register or unregister the context menu.");
WriteError("You have to run as admin to register or unregister the context menu.");
Environment.Exit(1);
}

private static void WriteError(string error)
{
Console.Error.WriteLine(error);
Console.Error.WriteLine("Press a key to continue");
Console.ReadKey();
}

private static void WriteSuccess(string message)
{
Console.WriteLine(message);
Console.WriteLine("Press a key to continue");
Console.ReadKey();
Environment.Exit(0);
}

/// <summary>
Expand All @@ -117,17 +122,18 @@ private static void RestartBinaryAsAdminIfRequired()
// Environment.Exit(0);
// }
//}

private static async Task VirusScanFile(string filePath)
{
JObject jObj = JObject.Parse(await File.ReadAllTextAsync("appsettings.json"));
string path = Path.Combine(Path.GetDirectoryName(Environment.ProcessPath)!, "appsettings.json");

Console.WriteLine(path);

JObject jObj = JObject.Parse(await File.ReadAllTextAsync(path));
string? key = jObj.GetValue("apikey")?.ToString();

if (string.IsNullOrWhiteSpace(key) || key.Length != 64)
{
Console.WriteLine("Invalid API key. Did you remember to change appsettings.json?");
Console.WriteLine("Press a key to continue");
Console.ReadKey();
WriteError("Invalid API key. Did you remember to change appsettings.json?");
return;
}

Expand Down Expand Up @@ -167,8 +173,15 @@ private static async Task VirusScanFile(string filePath)
}
else
{
Console.WriteLine($"Opening {report.Permalink}");
OpenUrl(report.Permalink);
if (string.IsNullOrEmpty(report.Permalink))
{
WriteError("No permalink associated with the file. Cannot open URL.");
}
else
{
Console.WriteLine($"Opening {report.Permalink}");
OpenUrl(report.Permalink);
}
}
}
}
10 changes: 5 additions & 5 deletions src/VirusTotalContextMenu.csproj
Expand Up @@ -2,10 +2,10 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<AssemblyVersion>1.7.0.0</AssemblyVersion>
<FileVersion>1.7.0.0</FileVersion>
<AssemblyVersion>1.8.0.0</AssemblyVersion>
<FileVersion>1.8.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -22,11 +22,11 @@
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
<EventSourceSupport>false</EventSourceSupport>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="VirusTotalNet" Version="2.1.0" />
<PackageReference Include="VirusTotalNet" Version="2.2.0" />
<TrimmerRootAssembly Include="VirusTotalNet" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit f1f9def

Please sign in to comment.