Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
Win Explorer Content Menu Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Byron Penner committed Sep 7, 2015
1 parent aabda47 commit 37369f0
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 6,188 deletions.
112 changes: 112 additions & 0 deletions CommonApplicationData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//Author: DaveyM69
//Date: 22 May 2010
//Link: http://www.codeproject.com/Tips/61987/Allow-write-modify-access-to-CommonApplicationData

using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace FileHasher
{
class CommonApplicationData
{

private string applicationFolder;
private string companyFolder;
private static readonly string directory =
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

/// <summary>
/// Creates a new instance of this class creating the specified company and application folders
/// if they don't already exist and optionally allows write/modify to all users.
/// </summary>
/// <param name="companyFolder">The name of the company's folder (normally the company name).</param>
/// <param name="applicationFolder">The name of the application's folder (normally the application name).</param>
/// <remarks>If the application folder already exists then permissions if requested are NOT altered.</remarks>
public CommonApplicationData(string companyFolder, string applicationFolder)
: this(companyFolder, applicationFolder, false)
{ }
/// <summary>
/// Creates a new instance of this class creating the specified company and application folders
/// if they don't already exist and optionally allows write/modify to all users.
/// </summary>
/// <param name="companyFolder">The name of the company's folder (normally the company name).</param>
/// <param name="applicationFolder">The name of the application's folder (normally the application name).</param>
/// <param name="allUsers">true to allow write/modify to all users; otherwise, false.</param>
/// <remarks>If the application folder already exists then permissions if requested are NOT altered.</remarks>
public CommonApplicationData(string companyFolder, string applicationFolder, bool allUsers)
{
this.applicationFolder = applicationFolder;
this.companyFolder = companyFolder;
CreateFolders(allUsers);
}

/// <summary>
/// Gets the path of the application's data folder.
/// </summary>
public string ApplicationFolderPath
{
get { return Path.Combine(CompanyFolderPath, applicationFolder); }
}
/// <summary>
/// Gets the path of the company's data folder.
/// </summary>
public string CompanyFolderPath
{
get { return Path.Combine(directory, companyFolder); }
}

private void CreateFolders(bool allUsers)
{
DirectoryInfo directoryInfo;
DirectorySecurity directorySecurity;
AccessRule rule;
SecurityIdentifier securityIdentifier = new SecurityIdentifier
(WellKnownSidType.BuiltinUsersSid, null);
if (!Directory.Exists(CompanyFolderPath))
{
directoryInfo = Directory.CreateDirectory(CompanyFolderPath);
bool modified;
directorySecurity = directoryInfo.GetAccessControl();
rule = new FileSystemAccessRule(
securityIdentifier,
FileSystemRights.Write |
FileSystemRights.ReadAndExecute |
FileSystemRights.Modify,
AccessControlType.Allow);
directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
directoryInfo.SetAccessControl(directorySecurity);
}
if (!Directory.Exists(ApplicationFolderPath))
{
directoryInfo = Directory.CreateDirectory(ApplicationFolderPath);
if (allUsers)
{
bool modified;
directorySecurity = directoryInfo.GetAccessControl();
rule = new FileSystemAccessRule(
securityIdentifier,
FileSystemRights.Write |
FileSystemRights.ReadAndExecute |
FileSystemRights.Modify,
InheritanceFlags.ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
directoryInfo.SetAccessControl(directorySecurity);
}
}
}
/// <summary>
/// Returns the path of the application's data folder.
/// </summary>
/// <returns>The path of the application's data folder.</returns>
public override string ToString()
{
return ApplicationFolderPath;
}

}
}
12 changes: 3 additions & 9 deletions FileHasher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="CommonApplicationData.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="GlobalSettings.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
Expand All @@ -108,18 +110,10 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="filehasher.pfx" />
<None Include="FileHasher_TemporaryKey.pfx" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="Resources\filehasher.png" />
Expand Down
17 changes: 16 additions & 1 deletion Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 111 additions & 1 deletion Form1.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
using System;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Windows.Forms;

namespace FileHasher
{
public partial class Form1 : Form
{
public string arg;
public int HashType = 0;
public Form1()
{
InitializeComponent();
}

public Form1(string argument)
{
arg = argument;
InitializeComponent();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (string.IsNullOrEmpty((string)e.Argument))
Expand Down Expand Up @@ -124,6 +134,14 @@ private void Form1_Load(object sender, EventArgs e)
saveFileDialog1.Title = Properties.Resources.SaveDialogTitle;

openFileDialog1.FileName = "";

enableCMenuCheckBox.Checked = GlobalSettings.WinContextMenuEnabled;

if (!string.IsNullOrEmpty(arg))
{
filenameTextBox.Text = arg;
computeButton_Click(sender, e);
}
}

private void copyButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -177,5 +195,97 @@ private void compareTextBox_TextChanged(object sender, EventArgs e)
compareTextBox.BackColor = System.Drawing.Color.LightCoral;

}

private void enableCMenuCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!IsAdmin())
{
RestartElevated(arg);
return;
}

if (enableCMenuCheckBox.Checked)
{
RegistryKey regmenu = null;
RegistryKey regcmd = null;
try
{
regmenu = Registry.ClassesRoot.CreateSubKey("*\\shell\\Checksum");
if (regmenu != null)
regmenu.SetValue("", "Checksum");
regcmd = Registry.ClassesRoot.CreateSubKey("*\\shell\\Checksum\\command");
if (regcmd != null)
regcmd.SetValue("", Application.ExecutablePath + " %1");

GlobalSettings.WinContextMenuEnabled = true;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
}

}
else
{
try
{
RegistryKey reg = Registry.ClassesRoot.OpenSubKey("*\\shell\\Checksum\\command");
if (reg != null)
{
reg.Close();
Registry.ClassesRoot.DeleteSubKey("*\\shell\\Checksum\\command");
}
reg = Registry.ClassesRoot.OpenSubKey("*\\shell\\Checksum");
if (reg != null)
{
reg.Close();
Registry.ClassesRoot.DeleteSubKey("*\\shell\\Checksum");
}

GlobalSettings.WinContextMenuEnabled = false;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
}

Application.DoEvents();
enableCMenuCheckBox.Checked = GlobalSettings.WinContextMenuEnabled;
}

static internal bool IsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);
return p.IsInRole(WindowsBuiltInRole.Administrator);
}

internal static void RestartElevated(string arg)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = arg;
startInfo.Verb = "runas";
try
{
Process p = Process.Start(startInfo);
}
catch (System.ComponentModel.Win32Exception ex)
{
return;
}

Application.Exit();
}
}
}
Loading

0 comments on commit 37369f0

Please sign in to comment.