Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
urholaukkarinen committed Apr 19, 2022
0 parents commit cfc3a93
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/

*.wixobj
*.wixpdb
*.msi
16 changes: 16 additions & 0 deletions AddToPath.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddToPath", "AddToPath\AddToPath.csproj", "{72C77FA0-8E74-44BD-93DA-898A0F1687E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{72C77FA0-8E74-44BD-93DA-898A0F1687E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72C77FA0-8E74-44BD-93DA-898A0F1687E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72C77FA0-8E74-44BD-93DA-898A0F1687E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72C77FA0-8E74-44BD-93DA-898A0F1687E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
86 changes: 86 additions & 0 deletions AddToPath/AddToPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;

namespace AddToPath
{
[ComVisible(true)]
[COMServerAssociation(AssociationType.Directory)]
public class AddToPath : SharpContextMenu
{
private string SelectedItemPath => SelectedItemPaths.First();
private string ItemRegexPattern => $"(?<=^|;){Regex.Escape(SelectedItemPath)}(?=$|;)";

protected override bool CanShowMenu()
{
return true;
}

protected override ContextMenuStrip CreateMenu()
{
var menu = new ContextMenuStrip();
var isInPath = IsInPath();

var itemCountLines = new ToolStripMenuItem()
{
Text = isInPath ? "Remove from PATH (User)" : "Add to PATH (User)",
};

itemCountLines.Click += (sender, args) =>
{
if (isInPath)
{
RunInSeparateThread(RemoveSelectedItemFromPath);
}
else
{
RunInSeparateThread(AddSelectedItemToPath);
}
};

menu.Items.Add(itemCountLines);

return menu;
}

private void RunInSeparateThread(ThreadStart start)
{
var worker = new Thread(start);
worker.SetApartmentState(ApartmentState.STA);
worker.Start();
}
private void AddSelectedItemToPath()
{
var oldPath = GetPath();
var newPath = $"{oldPath};{SelectedItemPath}";
SetPath(newPath);
}

private void RemoveSelectedItemFromPath()
{
var oldPath = GetPath();
var newPath = Regex.Replace(oldPath, ItemRegexPattern, "");
SetPath(newPath);
}

private bool IsInPath()
{
return Regex.Match(GetPath(), ItemRegexPattern).Success;
}

private static string GetPath()
{
return Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.User);
}

private static void SetPath(string newPath)
{
Environment.SetEnvironmentVariable("Path", newPath, EnvironmentVariableTarget.User);
}
}
}
61 changes: 61 additions & 0 deletions AddToPath/AddToPath.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{72C77FA0-8E74-44BD-93DA-898A0F1687E5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AddToPath</RootNamespace>
<AssemblyName>AddToPath</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>addtopath.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AddToPath.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="SharpShell" Version="2.7.2" />
</ItemGroup>
<ItemGroup>
<None Include="addtopath.snk" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
35 changes: 35 additions & 0 deletions AddToPath/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AddToPath")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AddToPath")]
[assembly: AssemblyCopyright("Copyright © Urho Laukkarinen 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("72C77FA0-8E74-44BD-93DA-898A0F1687E5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file added AddToPath/addtopath.snk
Binary file not shown.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# AddToPath

With this tool you can quickly add and remove directories from the PATH environment variable straight from the context menu.

## Installation

Download the latest installer from [Releases](https://github.com/urholaukkarinen/AddToPath/releases)

---

![Add to path](./addtopath.png)
![Remove from path](./removefrompath.png)
Binary file added addtopath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extras/srm.exe
Binary file not shown.
55 changes: 55 additions & 0 deletions installer.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version='1.0' encoding='windows-1252'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>

<Product Name='AddToPath' Id='*'
UpgradeCode='358c7069-7def-49d6-9fd4-cf966dd15eb0' Language='1033' Version='0.1.0' Manufacturer='Me'>

<Package Id='*' Keywords='Installer' Description="Add directories to PATH via context menu"
Manufacturer='Me' InstallerVersion='400' Languages='1033'
Compressed='yes' SummaryCodepage='1252' InstallScope='perMachine'
Platform='x64'/>

<Media Id='1' Cabinet='AddToPath.cab' EmbedCab='yes'/>

<Directory Id='TARGETDIR' Name='SourceDir'>
<Directory Id='ProgramFilesFolder' Name='PFiles'>
<Directory Id='INSTALLDIR' Name='AddToPath'>
<Component Id='Libraries' Guid='950328cd-3c67-446f-a259-53fd8e1d5b51'>
<File Id='AddToPath.dll' Name='AddToPath.dll'
Source='AddToPath\bin\Release\AddToPath.dll'
DiskId='1' />
<File Id='SharpShell.dll' Name='SharpShell.dll'
Source='AddToPath\bin\Release\SharpShell.dll'
DiskId='1' />
<File Id="SrmExe" Source="extras\srm.exe"/>
</Component>
</Directory>
</Directory>
</Directory>

<CustomAction Id="InstallShell" FileKey="SrmExe"
ExeCommand='install "[INSTALLDIR]\AddToPath.dll" -codebase'
Execute="deferred" Return="check" Impersonate="no" />
<CustomAction Id="UninstallShell" FileKey="SrmExe"
ExeCommand='uninstall "[INSTALLDIR]\AddToPath.dll"'
Execute="deferred" Return="check" Impersonate="no" />

<InstallExecuteSequence>
<Custom Action="InstallShell"
After="InstallFiles">NOT Installed
</Custom>
<Custom Action="UninstallShell"
Before="RemoveFiles">(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")
</Custom>
</InstallExecuteSequence>


<Feature Id='AddToPathFeature' Title='AddToPath' Description='AddToPath'
Level='1' AllowAdvertise='no'>
<ComponentRef Id="Libraries" />
</Feature>

<Property Id="MSIUSEREALADMINDETECTION" Value="1" />
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />
</Product>
</Wix>
Binary file added removefrompath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cfc3a93

Please sign in to comment.