ShareX/ShareX/StartupManager.cs

109 lines
3.4 KiB
C#
Raw Normal View History

2017-10-05 12:17:29 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2017-10-05 12:17:29 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Microsoft.Win32;
2017-10-05 12:17:29 +13:00
using ShareX.HelpersLib;
2017-10-05 11:21:32 +13:00
using System;
2024-04-12 05:42:10 +12:00
using System.Windows.Forms;
#if MicrosoftStore
using Windows.ApplicationModel;
#endif
2017-10-05 11:21:32 +13:00
2017-12-06 03:59:18 +13:00
namespace ShareX
2017-10-05 11:21:32 +13:00
{
2024-04-12 05:42:10 +12:00
public static class StartupManager
2017-10-05 11:21:32 +13:00
{
2024-04-12 05:42:10 +12:00
#if MicrosoftStore
private const int StartupTargetIndex = 0;
private static readonly StartupTask packageTask = StartupTask.GetForCurrentPackageAsync().GetAwaiter().GetResult()[StartupTargetIndex];
#endif
2017-10-05 12:26:44 +13:00
2024-04-12 05:42:10 +12:00
public static string StartupTargetPath
2017-10-05 11:21:32 +13:00
{
2017-10-05 12:26:44 +13:00
get
{
2024-04-12 05:42:10 +12:00
#if STEAM
return FileHelpers.GetAbsolutePath("../ShareX_Launcher.exe");
#else
return Application.ExecutablePath;
#endif
}
}
public static StartupState State
{
get
{
#if MicrosoftStore
return (StartupState)packageTask.State;
#else
2021-12-20 12:11:04 +13:00
if (ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, "ShareX", StartupTargetPath))
{
2021-06-10 10:14:01 +12:00
if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\StartupFolder",
"ShareX.lnk", null) is byte[] status && status.Length > 0 && status[0] == 3)
{
2018-10-02 08:08:23 +13:00
return StartupState.DisabledByUser;
}
else
{
2018-10-02 08:08:23 +13:00
return StartupState.Enabled;
}
}
else
{
2018-10-02 08:08:23 +13:00
return StartupState.Disabled;
}
2024-04-12 05:42:10 +12:00
#endif
2017-10-05 12:26:44 +13:00
}
2017-10-05 11:21:32 +13:00
set
{
2024-04-12 05:42:10 +12:00
#if MicrosoftStore
if (value == StartupState.Enabled)
{
packageTask.RequestEnableAsync().GetAwaiter().GetResult();
}
else if (value == StartupState.Disabled)
{
packageTask.Disable();
}
else
{
throw new NotSupportedException();
}
#else
2018-10-02 08:08:23 +13:00
if (value == StartupState.Enabled || value == StartupState.Disabled)
2017-10-05 11:21:32 +13:00
{
2021-12-20 12:11:04 +13:00
ShortcutHelpers.SetShortcut(value == StartupState.Enabled, Environment.SpecialFolder.Startup, "ShareX", StartupTargetPath, "-silent");
2017-10-05 11:21:32 +13:00
}
else
{
throw new NotSupportedException();
}
2024-04-12 05:42:10 +12:00
#endif
2017-10-05 11:21:32 +13:00
}
}
}
2024-04-12 05:42:10 +12:00
}