forked from sureshdr/mhora
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Link.cs
133 lines (123 loc) · 5.28 KB
/
Link.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/******
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******/
// =====================================================================
//
// ShellLink - Using WSH to program shell links
//
// by Jim Hollenhorst, [email protected]
// Copyright Ultrapico, April 2003
// http://www.ultrapico.com
//
// =====================================================================
using System;
using System.Windows.Forms;
using System.IO;
using IWshRuntimeLibrary;
namespace mhora
{
/// <summary>
/// Summary description for Link.
/// </summary>
public class Link
{
/// <summary>
/// Check to see if a shortcut exists in a given directory with a specified file name
/// </summary>
/// <param name="DirectoryPath">The directory in which to look</param>
/// <param name="FullPathName">The name of the shortcut (without the .lnk extension) or the full path to a file of the same name</param>
/// <returns>Returns true if the link exists</returns>
public static bool Exists(string DirectoryPath, string LinkPathName)
{
// Get some file and directory information
DirectoryInfo SpecialDir=new DirectoryInfo(DirectoryPath);
// First get the filename for the original file and create a new file
// name for a link in the Startup directory
//
FileInfo originalfile = new FileInfo(LinkPathName);
string NewFileName = SpecialDir.FullName+"\\"+originalfile.Name+".lnk";
FileInfo linkfile = new FileInfo(NewFileName);
return linkfile.Exists;
}
//Check to see if a shell link exists to the given path in the specified special folder
// return true if it exists
public static bool Exists(Environment.SpecialFolder folder, string LinkPathName)
{
return Link.Exists(Environment.GetFolderPath(folder), LinkPathName);
}
/// <summary>
/// Update the specified folder by creating or deleting a Shell Link if necessary
/// </summary>
/// <param name="folder">A SpecialFolder in which the link will reside</param>
/// <param name="TargetPathName">The path name of the target file for the link</param>
/// <param name="LinkPathName">The file name for the link itself or, if a path name the directory information will be ignored.</param>
/// <param name="Create">If true, create the link, otherwise delete it</param>
public static void Update(Environment.SpecialFolder folder, string TargetPathName, string LinkPathName, bool install)
{
// Get some file and directory information
Link.Update(Environment.GetFolderPath(folder), TargetPathName, LinkPathName, install);
}
// boolean variable "install" determines whether the link should be there or not.
// Update the folder by creating or deleting the link as required.
/// <summary>
/// Update the specified folder by creating or deleting a Shell Link if necessary
/// </summary>
/// <param name="DirectoryPath">The full path of the directory in which the link will reside</param>
/// <param name="TargetPathName">The path name of the target file for the link</param>
/// <param name="LinkPathName">The file name for the link itself or, if a path name the directory information will be ignored.</param>
/// <param name="Create">If true, create the link, otherwise delete it</param>
public static void Update(string DirectoryPath, string TargetPathName, string LinkPathName, bool Create)
{
// Get some file and directory information
DirectoryInfo SpecialDir=new DirectoryInfo(DirectoryPath);
// First get the filename for the original file and create a new file
// name for a link in the Startup directory
//
FileInfo OriginalFile = new FileInfo(LinkPathName);
string NewFileName = SpecialDir.FullName+"\\"+OriginalFile.Name+".lnk";
FileInfo LinkFile = new FileInfo(NewFileName);
if(Create) // If the link doesn't exist, create it
{
if(LinkFile.Exists)return; // We're all done if it already exists
//Place a shortcut to the file in the special folder
try
{
// Create a shortcut in the special folder for the file
// Making use of the Windows Scripting Host
WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkFile.FullName);
link.TargetPath=TargetPathName;
link.Save();
}
catch
{
MessageBox.Show("Unable to create link in special directory: "+NewFileName,
"Shell Link Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else // otherwise delete it from the startup directory
{
if(!LinkFile.Exists)return; // It doesn't exist so we are done!
try
{
LinkFile.Delete();
}
catch
{
MessageBox.Show("Error deleting link in special directory: "+NewFileName,
"Shell Link Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
}