2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2013-11-03 23:53:49 +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)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2014-10-17 22:58:15 +13:00
|
|
|
|
using ShareX.Properties;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.UploadersLib;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
2016-06-25 03:33:45 +12:00
|
|
|
|
using System.Collections.Generic;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2018-08-04 02:24:26 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Windows.Forms;
|
2018-10-14 23:57:04 +13:00
|
|
|
|
|
2018-09-17 09:46:32 +12:00
|
|
|
|
#if WindowsStore
|
|
|
|
|
using Windows.ApplicationModel;
|
|
|
|
|
using Windows.ApplicationModel.Activation;
|
|
|
|
|
#endif
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
namespace ShareX
|
|
|
|
|
{
|
|
|
|
|
internal static class Program
|
|
|
|
|
{
|
2018-11-14 20:08:45 +13:00
|
|
|
|
public const string Name = "ShareX";
|
|
|
|
|
|
|
|
|
|
public const ShareXBuild Build =
|
2017-04-23 05:38:19 +12:00
|
|
|
|
#if RELEASE
|
2018-11-14 20:08:45 +13:00
|
|
|
|
ShareXBuild.Release;
|
2017-04-22 03:23:52 +12:00
|
|
|
|
#elif STEAM
|
2018-11-14 20:08:45 +13:00
|
|
|
|
ShareXBuild.Steam;
|
2017-04-22 03:23:52 +12:00
|
|
|
|
#elif WindowsStore
|
2018-11-14 20:08:45 +13:00
|
|
|
|
ShareXBuild.MicrosoftStore;
|
2017-04-23 05:38:19 +12:00
|
|
|
|
#elif DEBUG
|
2018-11-14 20:08:45 +13:00
|
|
|
|
ShareXBuild.Debug;
|
2015-09-16 11:57:27 +12:00
|
|
|
|
#else
|
2018-11-14 20:08:45 +13:00
|
|
|
|
ShareXBuild.Unknown;
|
2015-09-10 08:58:31 +12:00
|
|
|
|
#endif
|
|
|
|
|
|
2018-11-14 20:08:45 +13:00
|
|
|
|
public static string VersionText
|
2014-09-14 01:50:44 +12:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-11-14 20:08:45 +13:00
|
|
|
|
StringBuilder sbVersionText = new StringBuilder();
|
2014-09-14 01:50:44 +12:00
|
|
|
|
Version version = Version.Parse(Application.ProductVersion);
|
2018-11-14 20:08:45 +13:00
|
|
|
|
sbVersionText.Append(version.Major + "." + version.Minor);
|
|
|
|
|
if (version.Build > 0) sbVersionText.Append("." + version.Build);
|
|
|
|
|
if (version.Revision > 0) sbVersionText.Append("." + version.Revision);
|
|
|
|
|
if (Dev) sbVersionText.Append(" Dev");
|
|
|
|
|
if (Portable) sbVersionText.Append(" Portable");
|
|
|
|
|
return sbVersionText.ToString();
|
2014-09-14 01:50:44 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 20:08:45 +13:00
|
|
|
|
public static string Title => $"{Name} {VersionText}";
|
|
|
|
|
|
2016-07-15 08:35:23 +12:00
|
|
|
|
public static string TitleLong => $"{Title} ({Build})";
|
2015-10-18 00:37:40 +13:00
|
|
|
|
|
2020-03-12 02:18:45 +13:00
|
|
|
|
public static bool Dev { get; } = true;
|
2016-06-25 04:49:06 +12:00
|
|
|
|
public static bool MultiInstance { get; private set; }
|
|
|
|
|
public static bool Portable { get; private set; }
|
|
|
|
|
public static bool PortableApps { get; private set; }
|
|
|
|
|
public static bool SilentRun { get; private set; }
|
|
|
|
|
public static bool Sandbox { get; private set; }
|
|
|
|
|
public static bool SteamFirstTimeConfig { get; private set; }
|
2016-06-25 05:24:24 +12:00
|
|
|
|
public static bool IgnoreHotkeyWarning { get; private set; }
|
2016-06-25 04:49:06 +12:00
|
|
|
|
public static bool PuushMode { get; private set; }
|
2014-09-14 01:50:44 +12:00
|
|
|
|
|
2017-03-27 04:19:52 +13:00
|
|
|
|
internal static ApplicationConfig Settings { get; set; }
|
|
|
|
|
internal static TaskSettings DefaultTaskSettings { get; set; }
|
|
|
|
|
internal static UploadersConfig UploadersConfig { get; set; }
|
|
|
|
|
internal static HotkeysConfig HotkeysConfig { get; set; }
|
2014-09-14 01:50:44 +12:00
|
|
|
|
|
2017-03-27 04:19:52 +13:00
|
|
|
|
internal static MainForm MainForm { get; private set; }
|
|
|
|
|
internal static Stopwatch StartTimer { get; private set; }
|
|
|
|
|
internal static HotkeyManager HotkeyManager { get; set; }
|
|
|
|
|
internal static WatchFolderManager WatchFolderManager { get; set; }
|
|
|
|
|
internal static GitHubUpdateManager UpdateManager { get; private set; }
|
|
|
|
|
internal static CLIManager CLI { get; private set; }
|
2014-09-14 01:50:44 +12:00
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
#region Paths
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-04-03 21:37:33 +12:00
|
|
|
|
private const string PersonalPathConfigFileName = "PersonalPath.cfg";
|
|
|
|
|
|
2018-11-14 20:08:45 +13:00
|
|
|
|
public static readonly string DefaultPersonalFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Name);
|
|
|
|
|
public static readonly string PortablePersonalFolder = Helpers.GetAbsolutePath(Name);
|
2018-08-27 11:00:54 +12:00
|
|
|
|
public static readonly string PortableAppsPersonalFolder = Helpers.GetAbsolutePath(@"..\..\Data");
|
2016-04-03 11:09:59 +12:00
|
|
|
|
|
|
|
|
|
private static string PersonalPathConfigFilePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-08-27 10:38:11 +12:00
|
|
|
|
string relativePath = Helpers.GetAbsolutePath(PersonalPathConfigFileName);
|
2016-04-03 21:37:33 +12:00
|
|
|
|
|
2018-08-27 10:38:11 +12:00
|
|
|
|
if (File.Exists(relativePath))
|
2016-04-03 21:37:33 +12:00
|
|
|
|
{
|
2018-08-27 10:38:11 +12:00
|
|
|
|
return relativePath;
|
2016-04-03 21:37:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:26:01 +12:00
|
|
|
|
return CurrentPersonalPathConfigFilePath;
|
2016-04-03 11:09:59 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:26:01 +12:00
|
|
|
|
private static readonly string CurrentPersonalPathConfigFilePath = Path.Combine(DefaultPersonalFolder, PersonalPathConfigFileName);
|
|
|
|
|
|
|
|
|
|
private static readonly string PreviousPersonalPathConfigFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
2018-11-14 20:08:45 +13:00
|
|
|
|
Name, PersonalPathConfigFileName);
|
2017-04-25 10:26:01 +12:00
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
private static readonly string PortableCheckFilePath = Helpers.GetAbsolutePath("Portable");
|
2016-01-08 06:48:59 +13:00
|
|
|
|
private static readonly string PortableAppsCheckFilePath = Helpers.GetAbsolutePath("PortableApps");
|
2017-02-11 03:43:29 +13:00
|
|
|
|
public static readonly string NativeMessagingHostFilePath = Helpers.GetAbsolutePath("ShareX_NativeMessagingHost.exe");
|
2015-10-27 15:03:24 +13:00
|
|
|
|
public static readonly string SteamInAppFilePath = Helpers.GetAbsolutePath("Steam");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
private static string CustomPersonalPath { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
public static string PersonalFolder
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-10-05 06:59:46 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(CustomPersonalPath))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-01-16 19:26:56 +13:00
|
|
|
|
return Helpers.ExpandFolderVariables(CustomPersonalPath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
return DefaultPersonalFolder;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 01:42:17 +13:00
|
|
|
|
public const string HistoryFilename = "History.json";
|
2017-12-18 21:20:37 +13:00
|
|
|
|
|
2017-03-27 08:21:10 +13:00
|
|
|
|
public static string HistoryFilePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Sandbox) return null;
|
|
|
|
|
|
2017-12-18 21:20:37 +13:00
|
|
|
|
return Path.Combine(PersonalFolder, HistoryFilename);
|
2017-03-27 08:21:10 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 01:42:17 +13:00
|
|
|
|
public const string HistoryFilenameOld = "History.xml";
|
|
|
|
|
|
|
|
|
|
public static string HistoryFilePathOld
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Sandbox) return null;
|
|
|
|
|
|
|
|
|
|
return Path.Combine(PersonalFolder, HistoryFilenameOld);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 21:20:37 +13:00
|
|
|
|
public const string LogsFoldername = "Logs";
|
|
|
|
|
|
|
|
|
|
public static string LogsFolder => Path.Combine(PersonalFolder, LogsFoldername);
|
2016-02-10 14:13:40 +13:00
|
|
|
|
|
2014-03-29 01:22:16 +13:00
|
|
|
|
public static string LogsFilePath
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-10-27 15:03:24 +13:00
|
|
|
|
string filename = string.Format("ShareX-Log-{0:yyyy-MM}.txt", DateTime.Now);
|
2016-02-10 14:13:40 +13:00
|
|
|
|
return Path.Combine(LogsFolder, filename);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 04:01:58 +13:00
|
|
|
|
public static string RequestLogsFilePath => Path.Combine(LogsFolder, "ShareX-Request-Logs.txt");
|
|
|
|
|
|
2014-05-02 18:12:51 +12:00
|
|
|
|
public static string ScreenshotsParentFolder
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-08-20 15:44:24 +12:00
|
|
|
|
if (Settings != null && Settings.UseCustomScreenshotsPath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-10-13 18:18:15 +13:00
|
|
|
|
string path = Settings.CustomScreenshotsPath;
|
|
|
|
|
string path2 = Settings.CustomScreenshotsPath2;
|
2016-08-20 15:44:24 +12:00
|
|
|
|
|
2016-10-13 18:18:15 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
2016-08-20 20:58:11 +12:00
|
|
|
|
{
|
2016-10-13 18:18:15 +13:00
|
|
|
|
path = Helpers.ExpandFolderVariables(path);
|
2016-08-20 20:58:11 +12:00
|
|
|
|
|
2016-10-13 18:18:15 +13:00
|
|
|
|
if (string.IsNullOrEmpty(path2) || Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-20 20:58:11 +12:00
|
|
|
|
|
2016-10-13 18:18:15 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(path2))
|
2016-08-20 20:58:11 +12:00
|
|
|
|
{
|
2016-10-13 18:18:15 +13:00
|
|
|
|
path2 = Helpers.ExpandFolderVariables(path2);
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(path2))
|
|
|
|
|
{
|
|
|
|
|
return path2;
|
|
|
|
|
}
|
2016-08-20 20:58:11 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
return Path.Combine(PersonalFolder, "Screenshots");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 18:12:51 +12:00
|
|
|
|
public static string ScreenshotsFolder
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-06-06 07:47:36 +12:00
|
|
|
|
string subFolderName = NameParser.Parse(NameParserType.FolderPath, Settings.SaveImageSubFolderPattern);
|
2018-07-02 23:54:08 +12:00
|
|
|
|
string folderPath = Path.Combine(ScreenshotsParentFolder, subFolderName);
|
|
|
|
|
return Helpers.GetAbsolutePath(folderPath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 14:13:40 +13:00
|
|
|
|
public static string ToolsFolder => Path.Combine(PersonalFolder, "Tools");
|
2015-10-27 15:03:24 +13:00
|
|
|
|
public static string ScreenRecorderCacheFilePath => Path.Combine(PersonalFolder, "ScreenRecorder.avi");
|
|
|
|
|
public static string DefaultFFmpegFilePath => Path.Combine(ToolsFolder, "ffmpeg.exe");
|
|
|
|
|
public static string ChromeHostManifestFilePath => Path.Combine(ToolsFolder, "Chrome-host-manifest.json");
|
2017-02-11 04:24:20 +13:00
|
|
|
|
public static string FirefoxHostManifestFilePath => Path.Combine(ToolsFolder, "Firefox-host-manifest.json");
|
2015-10-01 23:28:08 +13:00
|
|
|
|
|
2018-08-27 10:38:11 +12:00
|
|
|
|
private static string PersonalPathDetectionMethod;
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
#endregion Paths
|
|
|
|
|
|
2018-10-16 23:02:26 +13:00
|
|
|
|
private static bool closeSequenceStarted, restarting;
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
[STAThread]
|
|
|
|
|
private static void Main(string[] args)
|
|
|
|
|
{
|
2019-08-30 19:39:55 +12:00
|
|
|
|
// Allow Visual Studio to break on exceptions in Debug builds
|
|
|
|
|
#if !DEBUG
|
|
|
|
|
// Add the event handler for handling UI thread exceptions to the event
|
2013-11-03 23:53:49 +13:00
|
|
|
|
Application.ThreadException += Application_ThreadException;
|
2019-08-30 19:39:55 +12:00
|
|
|
|
|
|
|
|
|
// Set the unhandled exception mode to force all Windows Forms errors to go through our handler
|
2019-07-28 20:13:45 +12:00
|
|
|
|
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
|
2019-08-30 19:39:55 +12:00
|
|
|
|
|
|
|
|
|
// Add the event handler for handling non-UI thread exceptions to the event
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
2018-04-30 03:02:13 +12:00
|
|
|
|
#endif
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-08-08 14:10:49 +12:00
|
|
|
|
StartTimer = Stopwatch.StartNew(); // For be able to show startup time
|
|
|
|
|
|
2014-11-07 00:27:23 +13:00
|
|
|
|
CLI = new CLIManager(args);
|
2014-11-07 01:40:42 +13:00
|
|
|
|
CLI.ParseCommands();
|
2014-09-07 07:13:21 +12:00
|
|
|
|
|
2015-09-12 11:05:31 +12:00
|
|
|
|
#if STEAM
|
|
|
|
|
if (CheckUninstall()) return; // Steam will run ShareX with -Uninstall when uninstalling
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-09-07 07:13:21 +12:00
|
|
|
|
if (CheckAdminTasks()) return; // If ShareX opened just for be able to execute task as Admin
|
|
|
|
|
|
2015-11-22 14:31:48 +13:00
|
|
|
|
UpdatePersonalPath();
|
|
|
|
|
|
|
|
|
|
DebugHelper.Init(LogsFilePath);
|
|
|
|
|
|
2016-06-25 04:49:06 +12:00
|
|
|
|
MultiInstance = CLI.IsCommandExist("multi", "m");
|
2014-08-08 14:10:49 +12:00
|
|
|
|
|
2016-06-25 04:49:06 +12:00
|
|
|
|
using (ApplicationInstanceManager instanceManager = new ApplicationInstanceManager(!MultiInstance, args, SingleInstanceCallback))
|
2014-09-11 20:45:30 +12:00
|
|
|
|
{
|
2016-03-17 22:03:08 +13:00
|
|
|
|
Run();
|
|
|
|
|
}
|
2014-10-17 06:02:09 +13:00
|
|
|
|
|
2016-03-17 22:03:08 +13:00
|
|
|
|
if (restarting)
|
|
|
|
|
{
|
2017-03-27 05:10:52 +13:00
|
|
|
|
DebugHelper.WriteLine("ShareX restarting.");
|
2016-03-17 22:03:08 +13:00
|
|
|
|
Process.Start(Application.ExecutablePath);
|
2014-09-11 10:01:51 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-13 23:52:12 +12:00
|
|
|
|
private static void Run()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-10-05 06:59:46 +13:00
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
2018-11-14 20:08:45 +13:00
|
|
|
|
DebugHelper.WriteLine("ShareX starting.");
|
|
|
|
|
DebugHelper.WriteLine("Version: " + VersionText);
|
2016-06-25 03:33:45 +12:00
|
|
|
|
DebugHelper.WriteLine("Build: " + Build);
|
|
|
|
|
DebugHelper.WriteLine("Command line: " + Environment.CommandLine);
|
2018-11-14 20:08:45 +13:00
|
|
|
|
DebugHelper.WriteLine("Personal path: " + PersonalFolder);
|
2018-08-27 10:38:11 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(PersonalPathDetectionMethod))
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteLine("Personal path detection method: " + PersonalPathDetectionMethod);
|
|
|
|
|
}
|
2018-11-14 20:08:45 +13:00
|
|
|
|
DebugHelper.WriteLine("Operating system: " + Helpers.GetOperatingSystemProductName(true));
|
2018-11-17 00:13:50 +13:00
|
|
|
|
DebugHelper.WriteLine("Running as elevated process: " + Helpers.IsAdministrator());
|
2016-06-25 03:33:45 +12:00
|
|
|
|
|
2018-10-02 05:49:37 +13:00
|
|
|
|
SilentRun = CLI.IsCommandExist("silent", "s");
|
2018-09-17 09:46:32 +12:00
|
|
|
|
#if WindowsStore
|
2018-11-17 07:24:52 +13:00
|
|
|
|
SilentRun = SilentRun || AppInstance.GetActivatedEventArgs()?.Kind == ActivationKind.StartupTask;
|
2018-09-17 09:46:32 +12:00
|
|
|
|
#endif
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-30 05:58:38 +13:00
|
|
|
|
#if STEAM
|
2016-06-25 04:49:06 +12:00
|
|
|
|
SteamFirstTimeConfig = CLI.IsCommandExist("SteamConfig");
|
2015-09-30 05:58:38 +13:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-25 05:24:24 +12:00
|
|
|
|
IgnoreHotkeyWarning = CLI.IsCommandExist("NoHotkeys");
|
2015-09-11 12:45:40 +12:00
|
|
|
|
|
2016-06-25 03:33:45 +12:00
|
|
|
|
CheckPuushMode();
|
2016-06-25 05:24:24 +12:00
|
|
|
|
DebugWriteFlags();
|
2016-07-23 04:07:23 +12:00
|
|
|
|
CleanTempFiles();
|
2014-10-03 12:19:04 +13:00
|
|
|
|
|
2017-03-27 08:21:10 +13:00
|
|
|
|
SettingManager.LoadInitialSettings();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2017-02-10 01:52:07 +13:00
|
|
|
|
Uploader.UpdateServicePointManager();
|
2018-05-19 05:39:01 +12:00
|
|
|
|
UpdateManager = new GitHubUpdateManager("ShareX", "ShareX", Dev, Portable);
|
2020-06-29 10:10:21 +12:00
|
|
|
|
|
2014-10-04 00:29:35 +13:00
|
|
|
|
LanguageHelper.ChangeLanguage(Settings.Language);
|
2020-06-16 16:27:32 +12:00
|
|
|
|
|
2020-06-29 10:10:21 +12:00
|
|
|
|
Helpers.TryFixHandCursor();
|
2017-03-27 05:10:52 +13:00
|
|
|
|
DebugHelper.WriteLine("MainForm init started.");
|
2014-09-14 01:50:44 +12:00
|
|
|
|
MainForm = new MainForm();
|
2017-03-27 05:10:52 +13:00
|
|
|
|
DebugHelper.WriteLine("MainForm init finished.");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-09-14 01:50:44 +12:00
|
|
|
|
Application.Run(MainForm);
|
|
|
|
|
|
2018-10-16 23:02:26 +13:00
|
|
|
|
CloseSequence();
|
|
|
|
|
}
|
2014-09-14 01:50:44 +12:00
|
|
|
|
|
2018-10-16 23:02:26 +13:00
|
|
|
|
public static void CloseSequence()
|
|
|
|
|
{
|
|
|
|
|
if (!closeSequenceStarted)
|
|
|
|
|
{
|
|
|
|
|
closeSequenceStarted = true;
|
|
|
|
|
|
|
|
|
|
DebugHelper.Logger.AsyncWrite = false;
|
|
|
|
|
DebugHelper.WriteLine("ShareX closing.");
|
|
|
|
|
|
|
|
|
|
if (WatchFolderManager != null) WatchFolderManager.Dispose();
|
|
|
|
|
SettingManager.SaveAllSettings();
|
|
|
|
|
|
|
|
|
|
DebugHelper.WriteLine("ShareX closed.");
|
|
|
|
|
}
|
2014-09-14 01:50:44 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-17 06:02:09 +13:00
|
|
|
|
public static void Restart()
|
|
|
|
|
{
|
|
|
|
|
restarting = true;
|
|
|
|
|
Application.Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 01:50:44 +12:00
|
|
|
|
private static void SingleInstanceCallback(object sender, InstanceCallbackEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (WaitFormLoad(5000))
|
|
|
|
|
{
|
|
|
|
|
Action d = () =>
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-09-14 01:50:44 +12:00
|
|
|
|
if (args.CommandLineArgs == null || args.CommandLineArgs.Length < 1)
|
|
|
|
|
{
|
|
|
|
|
if (MainForm.niTray != null && MainForm.niTray.Visible)
|
|
|
|
|
{
|
|
|
|
|
// Workaround for Windows startup tray icon bug
|
|
|
|
|
MainForm.niTray.Visible = false;
|
|
|
|
|
MainForm.niTray.Visible = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 05:37:54 +13:00
|
|
|
|
MainForm.ForceActivate();
|
2014-09-14 01:50:44 +12:00
|
|
|
|
}
|
|
|
|
|
else if (MainForm.Visible)
|
|
|
|
|
{
|
2016-03-10 05:37:54 +13:00
|
|
|
|
MainForm.ForceActivate();
|
2014-09-14 01:50:44 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 00:27:23 +13:00
|
|
|
|
CLIManager cli = new CLIManager(args.CommandLineArgs);
|
2014-11-07 01:40:42 +13:00
|
|
|
|
cli.ParseCommands();
|
2014-11-07 00:27:23 +13:00
|
|
|
|
MainForm.UseCommandLineArgs(cli.Commands);
|
2014-09-14 01:50:44 +12:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MainForm.InvokeSafe(d);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-09-14 01:50:44 +12:00
|
|
|
|
private static bool WaitFormLoad(int wait)
|
|
|
|
|
{
|
|
|
|
|
Stopwatch timer = Stopwatch.StartNew();
|
2014-08-07 14:22:45 +12:00
|
|
|
|
|
2014-09-14 01:50:44 +12:00
|
|
|
|
while (timer.ElapsedMilliseconds < wait)
|
|
|
|
|
{
|
|
|
|
|
if (MainForm != null && MainForm.IsReady) return true;
|
2014-08-07 14:22:45 +12:00
|
|
|
|
|
2014-09-14 01:50:44 +12:00
|
|
|
|
Thread.Sleep(10);
|
2014-08-07 14:22:45 +12:00
|
|
|
|
}
|
2014-09-14 01:50:44 +12:00
|
|
|
|
|
|
|
|
|
return false;
|
2014-08-07 13:16:42 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-11-22 14:31:48 +13:00
|
|
|
|
private static void UpdatePersonalPath()
|
|
|
|
|
{
|
2016-06-25 04:49:06 +12:00
|
|
|
|
Sandbox = CLI.IsCommandExist("sandbox");
|
2015-11-22 14:31:48 +13:00
|
|
|
|
|
2016-06-25 04:49:06 +12:00
|
|
|
|
if (!Sandbox)
|
2015-11-22 14:31:48 +13:00
|
|
|
|
{
|
2018-08-27 10:38:11 +12:00
|
|
|
|
if (CLI.IsCommandExist("portable", "p"))
|
2015-11-22 14:31:48 +13:00
|
|
|
|
{
|
2018-08-27 10:38:11 +12:00
|
|
|
|
Portable = true;
|
2015-11-22 14:31:48 +13:00
|
|
|
|
CustomPersonalPath = PortablePersonalFolder;
|
2018-08-27 10:38:11 +12:00
|
|
|
|
PersonalPathDetectionMethod = "Portable CLI flag";
|
|
|
|
|
}
|
|
|
|
|
else if (File.Exists(PortableCheckFilePath))
|
|
|
|
|
{
|
|
|
|
|
Portable = true;
|
|
|
|
|
CustomPersonalPath = PortablePersonalFolder;
|
|
|
|
|
PersonalPathDetectionMethod = $"Portable file ({PortableCheckFilePath})";
|
|
|
|
|
}
|
|
|
|
|
else if (File.Exists(PortableAppsCheckFilePath))
|
|
|
|
|
{
|
|
|
|
|
Portable = PortableApps = true;
|
|
|
|
|
CustomPersonalPath = PortableAppsPersonalFolder;
|
|
|
|
|
PersonalPathDetectionMethod = $"PortableApps file ({PortableAppsCheckFilePath})";
|
2015-11-22 14:31:48 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-08-27 10:38:11 +12:00
|
|
|
|
#if !WindowsStore
|
|
|
|
|
MigratePersonalPathConfig();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
string customPersonalPath = ReadPersonalPathConfig();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(customPersonalPath))
|
|
|
|
|
{
|
|
|
|
|
CustomPersonalPath = Helpers.GetAbsolutePath(customPersonalPath);
|
|
|
|
|
PersonalPathDetectionMethod = $"PersonalPath.cfg file ({PersonalPathConfigFilePath})";
|
|
|
|
|
}
|
2015-11-22 14:31:48 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(PersonalFolder))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(PersonalFolder);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-08-27 11:00:54 +12:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
sb.AppendFormat("{0} \"{1}\"", Resources.Program_Run_Unable_to_create_folder_, PersonalFolder);
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(PersonalPathDetectionMethod))
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine("Personal path detection method: " + PersonalPathDetectionMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
sb.Append(e);
|
|
|
|
|
|
|
|
|
|
MessageBox.Show(sb.ToString(), "ShareX - " + Resources.Program_Run_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2015-11-22 14:31:48 +13:00
|
|
|
|
CustomPersonalPath = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:26:01 +12:00
|
|
|
|
private static void MigratePersonalPathConfig()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(PreviousPersonalPathConfigFilePath))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-12-15 11:18:27 +13:00
|
|
|
|
if (!File.Exists(CurrentPersonalPathConfigFilePath))
|
|
|
|
|
{
|
|
|
|
|
Helpers.CreateDirectoryFromFilePath(CurrentPersonalPathConfigFilePath);
|
|
|
|
|
File.Move(PreviousPersonalPathConfigFilePath, CurrentPersonalPathConfigFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:26:01 +12:00
|
|
|
|
File.Delete(PreviousPersonalPathConfigFilePath);
|
|
|
|
|
Directory.Delete(Path.GetDirectoryName(PreviousPersonalPathConfigFilePath));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
e.ShowError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 07:36:23 +13:00
|
|
|
|
public static string ReadPersonalPathConfig()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-10-27 15:03:24 +13:00
|
|
|
|
if (File.Exists(PersonalPathConfigFilePath))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-10-27 15:03:24 +13:00
|
|
|
|
return File.ReadAllText(PersonalPathConfigFilePath, Encoding.UTF8).Trim();
|
2013-12-12 07:36:23 +13:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-05-25 06:15:45 +12:00
|
|
|
|
return "";
|
2013-12-12 07:36:23 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 00:51:37 +12:00
|
|
|
|
public static bool WritePersonalPathConfig(string path)
|
2013-12-12 07:36:23 +13:00
|
|
|
|
{
|
2014-03-11 14:23:18 +13:00
|
|
|
|
if (path == null)
|
2013-12-12 07:36:23 +13:00
|
|
|
|
{
|
2016-05-25 06:15:45 +12:00
|
|
|
|
path = "";
|
2014-03-11 14:23:18 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
path = path.Trim();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 15:03:24 +13:00
|
|
|
|
bool isDefaultPath = string.IsNullOrEmpty(path) && !File.Exists(PersonalPathConfigFilePath);
|
2014-03-11 14:23:18 +13:00
|
|
|
|
|
|
|
|
|
if (!isDefaultPath)
|
|
|
|
|
{
|
|
|
|
|
string currentPath = ReadPersonalPathConfig();
|
|
|
|
|
|
|
|
|
|
if (!path.Equals(currentPath, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-04-03 11:09:59 +12:00
|
|
|
|
Helpers.CreateDirectoryFromFilePath(PersonalPathConfigFilePath);
|
2015-10-27 15:03:24 +13:00
|
|
|
|
File.WriteAllText(PersonalPathConfigFilePath, path, Encoding.UTF8);
|
2018-08-26 00:51:37 +12:00
|
|
|
|
return true;
|
2014-03-11 14:23:18 +13:00
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
|
{
|
2015-10-27 15:03:24 +13:00
|
|
|
|
MessageBox.Show(string.Format(Resources.Program_WritePersonalPathConfig_Cant_access_to_file, PersonalPathConfigFilePath),
|
2014-10-17 22:58:15 +13:00
|
|
|
|
"ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
2014-03-11 14:23:18 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2018-08-26 00:51:37 +12:00
|
|
|
|
|
|
|
|
|
return false;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
OnError(e.Exception);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-13 23:52:12 +12:00
|
|
|
|
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
OnError((Exception)e.ExceptionObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnError(Exception e)
|
|
|
|
|
{
|
2015-10-18 00:37:40 +13:00
|
|
|
|
using (ErrorForm errorForm = new ErrorForm(e.Message, $"{e}\r\n\r\n{TitleLong}", LogsFilePath, Links.URL_ISSUES))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
errorForm.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-07 07:13:21 +12:00
|
|
|
|
private static bool CheckAdminTasks()
|
|
|
|
|
{
|
2014-11-07 00:27:23 +13:00
|
|
|
|
if (CLI.IsCommandExist("dnschanger"))
|
2014-09-07 07:13:21 +12:00
|
|
|
|
{
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
2018-04-30 03:30:03 +12:00
|
|
|
|
Helpers.TryFixHandCursor();
|
2014-09-07 07:13:21 +12:00
|
|
|
|
Application.Run(new DNSChangerForm());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-12 11:05:31 +12:00
|
|
|
|
|
|
|
|
|
private static bool CheckUninstall()
|
|
|
|
|
{
|
2015-09-12 11:38:10 +12:00
|
|
|
|
if (CLI.IsCommandExist("uninstall"))
|
2015-09-12 11:05:31 +12:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IntegrationHelpers.Uninstall();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-20 18:44:21 +12:00
|
|
|
|
|
|
|
|
|
private static bool CheckPuushMode()
|
|
|
|
|
{
|
|
|
|
|
string puushPath = Helpers.GetAbsolutePath("puush");
|
2016-06-25 04:49:06 +12:00
|
|
|
|
PuushMode = File.Exists(puushPath);
|
|
|
|
|
return PuushMode;
|
2016-06-20 18:44:21 +12:00
|
|
|
|
}
|
2016-06-25 03:33:45 +12:00
|
|
|
|
|
2016-06-25 05:24:24 +12:00
|
|
|
|
private static void DebugWriteFlags()
|
2016-06-25 03:33:45 +12:00
|
|
|
|
{
|
|
|
|
|
List<string> flags = new List<string>();
|
|
|
|
|
|
2018-05-19 05:39:01 +12:00
|
|
|
|
if (Dev) flags.Add(nameof(Dev));
|
2016-06-25 04:49:06 +12:00
|
|
|
|
if (MultiInstance) flags.Add(nameof(MultiInstance));
|
|
|
|
|
if (Portable) flags.Add(nameof(Portable));
|
|
|
|
|
if (PortableApps) flags.Add(nameof(PortableApps));
|
|
|
|
|
if (SilentRun) flags.Add(nameof(SilentRun));
|
|
|
|
|
if (Sandbox) flags.Add(nameof(Sandbox));
|
|
|
|
|
if (SteamFirstTimeConfig) flags.Add(nameof(SteamFirstTimeConfig));
|
2016-06-25 05:24:24 +12:00
|
|
|
|
if (IgnoreHotkeyWarning) flags.Add(nameof(IgnoreHotkeyWarning));
|
2016-06-25 04:49:06 +12:00
|
|
|
|
if (PuushMode) flags.Add(nameof(PuushMode));
|
2016-06-25 03:33:45 +12:00
|
|
|
|
|
|
|
|
|
string output = string.Join(", ", flags);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(output))
|
|
|
|
|
{
|
2016-06-25 05:24:24 +12:00
|
|
|
|
DebugHelper.WriteLine("Flags: " + output);
|
2016-06-25 03:33:45 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-23 04:07:23 +12:00
|
|
|
|
|
|
|
|
|
private static void CleanTempFiles()
|
|
|
|
|
{
|
2018-08-04 02:24:26 +12:00
|
|
|
|
Task.Run(() =>
|
2016-07-23 04:07:23 +12:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string tempFolder = Path.GetTempPath();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tempFolder))
|
|
|
|
|
{
|
|
|
|
|
string folderPath = Path.Combine(tempFolder, "ShareX");
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(folderPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(folderPath, true);
|
|
|
|
|
|
2017-03-27 05:10:52 +13:00
|
|
|
|
DebugHelper.WriteLine($"Temp files cleaned: {folderPath}");
|
2016-07-23 04:07:23 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2018-08-04 02:24:26 +12:00
|
|
|
|
});
|
2016-07-23 04:07:23 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|