Skip to content

Commit

Permalink
fix ExeMover
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Mar 12, 2024
1 parent 323b535 commit 8e23be0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
11 changes: 2 additions & 9 deletions Project-Aurora/ExeMover/ExeMover.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<AssemblyName>Aurora</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest>
Expand All @@ -16,15 +17,7 @@
</PropertyGroup>

<ItemGroup>
<COMReference Include="Shell32">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.1" />
</ItemGroup>

</Project>
79 changes: 44 additions & 35 deletions Project-Aurora/ExeMover/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Management.Automation;

// It is unfortunate but we have to set it to Unknown first.
Thread.CurrentThread.SetApartmentState(ApartmentState.Unknown);
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

const int csidlCommonStartmenu = 0x16; // All Users\Start Menu
const int csidlCommonStartmenu = 0x16; // All Users\Start Menu
const int csidlStartmenu = 0xb;
var shell = new Shell32.Shell();

var globalStartup = new StringBuilder(260);
SHGetSpecialFolderPath(IntPtr.Zero, globalStartup, csidlCommonStartmenu, false);
Expand All @@ -26,45 +22,58 @@
return;
}

string[] auroraShortcutPaths = [
var script = """
param (
[Parameter(Mandatory = $true)]
[string]$TargetPath,
[Parameter(Mandatory = $true)]
[string[]]$LnkFiles
)
foreach ($lnkFile in $LnkFiles) {
# Create a Shell object
$shell = New-Object -ComObject WScript.Shell
# Check if the file exists
if (Test-Path $lnkFile) {
# Get the shortcut object
$shortcut = $shell.CreateShortcut($lnkFile)
# Change the target path
$shortcut.TargetPath = $TargetPath
# Save the changes
$shortcut.Save()
Write-Output "Changed target for $($lnkFile) to $TargetPath"
} else {
Write-Output "File $($lnkFile) does not exist."
}
}
Write-Output "All .lnk files have been updated to target $TargetPath"
""";

var newPath = Path.Combine(currentPath, "AuroraRgb.exe");
string[] auroraShortcutPaths =
[
Path.Combine(globalStartPath, "Aurora.lnk"),
Path.Combine(userStartPath, "Aurora.lnk"),
Path.Combine(userStartPath, "Aurora - Shortcut.lnk"),
Path.Combine(userStartPath, "Aurora.exe - Shortcut.lnk"),
];
var lnkFiles = string.Join(", ", auroraShortcutPaths.Select(s => '"' + s + '"'));

var powershell = PowerShell.Create().AddScript(script);
powershell.AddParameter("TargetPath", newPath);
powershell.AddParameter("LnkFiles", lnkFiles);
powershell.Invoke();

foreach (var auroraShortcutPath in auroraShortcutPaths)
{
ChangeToNewPath(auroraShortcutPath);
}

Process.Start("AuroraRgb.exe");
return;

[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hndOwner,
[Out] StringBuilder lpszPath, int nFolder, bool fCreate);

void ChangeToNewPath(string shortcutPath)
{
var newPath = Path.Combine(currentPath, "AuroraRgb.exe");
if (!File.Exists(shortcutPath))
{
return;
}
ChangeLinkTarget(shortcutPath, newPath);
}

void ChangeLinkTarget(string shortcutFullPath, string newTarget)
{
// Load the shortcut.
var folder = shell.NameSpace(Path.GetDirectoryName(shortcutFullPath));
var folderItem = folder.Items().Item(Path.GetFileName(shortcutFullPath));
var currentLink = (Shell32.ShellLinkObject)folderItem.GetLink;

// Assign the new path here. This value is not read-only.
currentLink.Path = newTarget;

// Save the link to commit the changes.
currentLink.Save();
}

0 comments on commit 8e23be0

Please sign in to comment.