ShareX/ShareX.Steam/Helpers.cs

134 lines
4.2 KiB
C#

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
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 System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace ShareX.Steam
{
public static class Helpers
{
[DllImport("kernel32.dll")]
public static extern uint WinExec(string lpCmdLine, uint uCmdShow);
public static string GetAbsolutePath(string path)
{
if (!Path.IsPathRooted(path)) // Is relative path?
{
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
}
return Path.GetFullPath(path);
}
public static bool IsRunning(string name)
{
bool createdNew = true;
try
{
using (Mutex mutex = new Mutex(false, name, out createdNew))
{
}
}
catch
{
}
return !createdNew;
}
/// <summary>
/// If version1 newer than version2 = 1
/// If version1 equal to version2 = 0
/// If version1 older than version2 = -1
/// </summary>
public static int CompareVersion(string version1, string version2)
{
return ParseVersion(version1).CompareTo(ParseVersion(version2));
}
private static Version ParseVersion(string version)
{
return NormalizeVersion(Version.Parse(version));
}
private static Version NormalizeVersion(Version version)
{
return new Version(Math.Max(version.Major, 0), Math.Max(version.Minor, 0), Math.Max(version.Build, 0), Math.Max(version.Revision, 0));
}
public static void ShowError(Exception e)
{
MessageBox.Show(e.ToString(), "ShareX - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void CopyAll(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
if (!Directory.Exists(target.FullName))
{
Directory.CreateDirectory(target.FullName);
}
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
public static bool IsCommandExist(string[] args, string command)
{
if (args != null && !string.IsNullOrEmpty(command))
{
return args.Any(arg => !string.IsNullOrEmpty(arg) && arg.Equals(command, StringComparison.OrdinalIgnoreCase));
}
return false;
}
public static void CreateEmptyFile(string path)
{
File.Create(path).Dispose();
}
}
}