Skip to content

Commit

Permalink
fix ExeMover script permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Mar 22, 2024
1 parent 5075411 commit d8c05b9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
1 change: 0 additions & 1 deletion Project-Aurora/ExeMover/ExeMover.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<PublishSelfContained>true</PublishSelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>

<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down
118 changes: 73 additions & 45 deletions Project-Aurora/ExeMover/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
var userStartup = new StringBuilder(260);
SHGetSpecialFolderPath(IntPtr.Zero, userStartup, csidlStartmenu, false);

var globalStartPath = Path.Combine(globalStartup.ToString(), "Programs");
var userStartPath = Path.Combine(userStartup.ToString(), "Programs");
string[] startPaths =
[
Path.Combine(globalStartup.ToString(), "Programs"),
Path.Combine(userStartup.ToString(), "Programs")
];

string[] shortcutNames = ["Aurora.lnk", "Aurora - Shortcut.lnk", "Aurora.exe - Shortcut.lnk"];

var auroraShortcutPaths = (from startPath in startPaths
from shortcutName in shortcutNames
select Path.Combine(startPath, shortcutName)).ToList();

var currentPath = Path.GetDirectoryName(Environment.ProcessPath);
if (currentPath == null)
Expand All @@ -22,54 +31,73 @@
return;
}

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"
""";
const string script = """
param (
[Parameter(Mandatory = $true)]
[string]$TargetPath,
[Parameter(Mandatory = $true)]
[string[]]$LnkFiles
)
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
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();

powershell.Streams.Verbose.DataAdded += (_, eventArgs) =>
{
Console.WriteLine(powershell.Streams.Verbose[eventArgs.Index]);
};
powershell.Streams.Debug.DataAdded += (_, eventArgs) =>
{
Console.WriteLine(powershell.Streams.Debug[eventArgs.Index]);
};
powershell.Streams.Information.DataAdded += (_, eventArgs) =>
{
Console.WriteLine(powershell.Streams.Information[eventArgs.Index]);
};
powershell.Streams.Warning.DataAdded += (_, eventArgs) =>
{
Console.Error.WriteLine(powershell.Streams.Warning[eventArgs.Index]);
};
powershell.Streams.Error.DataAdded += (_, eventArgs) =>
{
Console.Error.WriteLine(powershell.Streams.Error[eventArgs.Index]);
};

powershell.AddParameter("TargetPath", newPath);
powershell.AddParameter("LnkFiles", auroraShortcutPaths);
var psObjects = powershell.Invoke();
foreach (var psObject in psObjects)
{
Console.WriteLine(psObject);
}

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

0 comments on commit d8c05b9

Please sign in to comment.