Check shortcut target path too

This commit is contained in:
Jaex 2015-10-12 11:09:02 +03:00
parent 0d4a8a8d3c
commit c67bfad28a
6 changed files with 97 additions and 35 deletions

View file

@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using IWshRuntimeLibrary;
using Shell32;
using System;
using System.IO;
using File = System.IO.File;
@ -32,7 +33,40 @@ namespace ShareX.HelpersLib
{
public static class ShortcutHelpers
{
public static bool Create(string shortcutPath, string targetPath, string arguments = "")
public static bool SetShortcut(bool create, Environment.SpecialFolder specialFolder, string targetPath = "", string arguments = "")
{
string shortcutPath = GetShortcutPath(specialFolder);
return SetShortcut(create, shortcutPath, targetPath, arguments);
}
public static bool SetShortcut(bool create, string shortcutPath, string targetPath = "", string arguments = "")
{
if (create)
{
return Create(shortcutPath, targetPath, arguments);
}
return Delete(shortcutPath);
}
public static bool CheckShortcut(Environment.SpecialFolder specialFolder, string checkPath)
{
string shortcutPath = GetShortcutPath(specialFolder);
return CheckShortcut(shortcutPath, checkPath);
}
public static bool CheckShortcut(string shortcutPath, string targetPath)
{
if (!string.IsNullOrEmpty(shortcutPath) && !string.IsNullOrEmpty(targetPath) && File.Exists(shortcutPath))
{
string checkPath = GetShortcutTargetPath(shortcutPath);
return !string.IsNullOrEmpty(checkPath) && checkPath.Equals(targetPath, StringComparison.InvariantCultureIgnoreCase);
}
return false;
}
private static bool Create(string shortcutPath, string targetPath, string arguments = "")
{
if (!string.IsNullOrEmpty(shortcutPath) && !string.IsNullOrEmpty(targetPath) && File.Exists(targetPath))
{
@ -56,7 +90,7 @@ public static bool Create(string shortcutPath, string targetPath, string argumen
return false;
}
public static bool Delete(string shortcutPath)
private static bool Delete(string shortcutPath)
{
if (!string.IsNullOrEmpty(shortcutPath) && File.Exists(shortcutPath))
{
@ -67,35 +101,35 @@ public static bool Delete(string shortcutPath)
return false;
}
public static bool SetShortcut(bool create, Environment.SpecialFolder specialFolder, string filepath = "", string arguments = "")
private static string GetShortcutTargetPath(string shortcutPath)
{
string shortcutPath = GetShortcutPath(specialFolder);
string directory = Path.GetDirectoryName(shortcutPath);
string filename = Path.GetFileName(shortcutPath);
if (create)
try
{
return Create(shortcutPath, filepath, arguments);
Shell shell = new ShellClass();
Shell32.Folder folder = shell.NameSpace(directory);
FolderItem folderItem = folder.ParseName(filename);
if (folderItem != null)
{
ShellLinkObject link = (ShellLinkObject)folderItem.GetLink;
return link.Path;
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
return Delete(shortcutPath);
}
public static bool CheckShortcut(Environment.SpecialFolder specialFolder)
{
string shortcutPath = GetShortcutPath(specialFolder);
return File.Exists(shortcutPath);
return null;
}
private static string GetShortcutPath(Environment.SpecialFolder specialFolder)
{
string folderPath = Environment.GetFolderPath(specialFolder);
string shortcutPath = Path.Combine(folderPath, "ShareX");
if (!Path.GetExtension(shortcutPath).Equals(".lnk", StringComparison.InvariantCultureIgnoreCase))
{
shortcutPath = Path.ChangeExtension(shortcutPath, "lnk");
}
return shortcutPath;
return Path.Combine(folderPath, "ShareX.lnk");
}
}
}

View file

@ -993,6 +993,15 @@
<Isolated>False</Isolated>
<EmbedInteropTypes>False</EmbedInteropTypes>
</COMReference>
<COMReference Include="Shell32">
<Guid>{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>False</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<None Include="Resources\Loading.gif" />

View file

@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
using System.IO;
@ -73,7 +74,7 @@ public override UploadResult Upload(Stream stream, string fileName)
{
if (Uploader == null || string.IsNullOrEmpty(Uploader.UploadURL))
{
Errors.Add("Please select one of Pomf uploaders from \"Destination settings window -> Pomf tab\".");
Errors.Add(Resources.Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_window____Pomf_tab__);
return null;
}

View file

@ -846,6 +846,16 @@ internal static System.Drawing.Icon Pomf {
}
}
/// <summary>
/// Looks up a localized string similar to Please select one of the Pomf uploaders from &quot;Destination settings window -&gt; Pomf tab&quot;..
/// </summary>
internal static string Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_window____Pomf_tab__ {
get {
return ResourceManager.GetString("Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_wi" +
"ndow____Pomf_tab__", resourceCulture);
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
/// </summary>

View file

@ -476,4 +476,7 @@ Created folders:</value>
<data name="Pomf" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Favicons\Pomf.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_window____Pomf_tab__" xml:space="preserve">
<value>Please select one of the Pomf uploaders from "Destination settings window -&gt; Pomf tab".</value>
</data>
</root>

View file

@ -33,24 +33,29 @@ namespace ShareX
{
public static class IntegrationHelpers
{
private static string GetStartupTargetPath()
{
string path;
#if STEAM
path = Helpers.GetAbsolutePath("../ShareX_Launcher.exe");
#else
path = Application.ExecutablePath;
#endif
return path;
}
public static bool CheckStartupShortcut()
{
return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup); //RegistryHelper.CheckStartWithWindows();
string targetPath = GetStartupTargetPath();
return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, targetPath);
}
public static void CreateStartupShortcut(bool create)
{
//RegistryHelper.SetStartWithWindows(cbStartWithWindows.Checked);
string filePath;
#if STEAM
filePath = Helpers.GetAbsolutePath("../ShareX_Launcher.exe");
#else
filePath = Application.ExecutablePath;
#endif
ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, filePath, "-silent");
string targetPath = GetStartupTargetPath();
ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, targetPath, "-silent");
}
public static bool CheckShellContextMenuButton()
@ -65,7 +70,7 @@ public static void CreateShellContextMenuButton(bool create)
public static bool CheckSendToMenuButton()
{
return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.SendTo);
return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.SendTo, Application.ExecutablePath);
}
public static void CreateSendToMenuButton(bool create)