ShareX/ShareX.HelpersLib/Helpers/ShortcutHelpers.cs

108 lines
3.4 KiB
C#
Raw Normal View History

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
2015-08-13 13:07:38 +12:00
Copyright (c) 2007-2015 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-01-05 06:40:03 +13:00
#if !__MonoCS__
2014-03-29 01:55:41 +13:00
2013-11-03 23:53:49 +13:00
using IWshRuntimeLibrary;
2014-03-29 01:55:41 +13:00
2014-01-05 06:40:03 +13:00
#endif
2014-03-29 01:55:41 +13:00
2013-11-03 23:53:49 +13:00
using System;
using System.IO;
using System.Windows.Forms;
using File = System.IO.File;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public static class ShortcutHelpers
{
public static bool Create(string shortcutPath, string targetPath, string arguments = "")
{
2014-01-05 06:40:03 +13:00
#if !__MonoCS__
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(shortcutPath) && !string.IsNullOrEmpty(targetPath) && File.Exists(targetPath))
{
try
{
IWshShell wsh = new WshShellClass();
IWshShortcut shortcut = (IWshShortcut)wsh.CreateShortcut(shortcutPath);
shortcut.TargetPath = targetPath;
shortcut.Arguments = arguments;
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
shortcut.Save();
return true;
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
2014-01-05 06:40:03 +13:00
#endif
2013-11-03 23:53:49 +13:00
return false;
}
public static bool Delete(string shortcutPath)
{
if (!string.IsNullOrEmpty(shortcutPath) && File.Exists(shortcutPath))
{
File.Delete(shortcutPath);
return true;
}
return false;
}
public static bool SetShortcut(bool create, Environment.SpecialFolder specialFolder, string arguments = "")
{
string shortcutPath = GetShortcutPath(specialFolder);
if (create)
{
return Create(shortcutPath, Application.ExecutablePath, arguments);
}
return Delete(shortcutPath);
}
public static bool CheckShortcut(Environment.SpecialFolder specialFolder)
{
string shortcutPath = GetShortcutPath(specialFolder);
return File.Exists(shortcutPath);
}
private static string GetShortcutPath(Environment.SpecialFolder specialFolder)
{
string folderPath = Environment.GetFolderPath(specialFolder);
string shortcutPath = Path.Combine(folderPath, "ShareX");
2013-11-03 23:53:49 +13:00
if (!Path.GetExtension(shortcutPath).Equals(".lnk", StringComparison.InvariantCultureIgnoreCase))
{
shortcutPath = Path.ChangeExtension(shortcutPath, "lnk");
}
return shortcutPath;
}
}
}