using System;
using System.Windows.Forms;
using System.Drawing;
namespace ShellLinks
{
/// <summary>
/// This is the shell links demonstration application
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button BtnExit;
private bool Skip;
private System.Windows.Forms.CheckBox chkRunOnStartup;
private System.Windows.Forms.CheckBox chkSendToLink;
private System.Windows.Forms.CheckBox chkQuickLaunch;
private System.Windows.Forms.CheckBox chkDesktopLink;
private string QuickLaunchDir;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Set check buttons depending on whether shortcuts exist on the desktop and in the startup folder
Skip=true; // Don't run the CheckedChanged code
chkRunOnStartup.Checked=Link.Exists(Environment.SpecialFolder.Startup,"Shell Link");
chkDesktopLink.Checked=Link.Exists(Environment.SpecialFolder.DesktopDirectory,"Shell Link");
chkSendToLink.Checked=Link.Exists(Environment.SpecialFolder.SendTo,"Shell Link");
QuickLaunchDir=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ "\\Microsoft\\Internet Explorer\\Quick Launch";
chkQuickLaunch.Checked=Link.Exists(QuickLaunchDir,"Shell Link");
Skip=false;
}
public void Exit()
{
Application.Exit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.chkDesktopLink = new System.Windows.Forms.CheckBox();
this.chkRunOnStartup = new System.Windows.Forms.CheckBox();
this.chkQuickLaunch = new System.Windows.Forms.CheckBox();
this.BtnExit = new System.Windows.Forms.Button();
this.chkSendToLink = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// chkDesktopLink
//
this.chkDesktopLink.Checked = true;
this.chkDesktopLink.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkDesktopLink.Location = new System.Drawing.Point(8, 64);
this.chkDesktopLink.Name = "chkDesktopLink";
this.chkDesktopLink.Size = new System.Drawing.Size(184, 24);
this.chkDesktopLink.TabIndex = 3;
this.chkDesktopLink.Text = "Put Shortcut on the Desktop";
this.chkDesktopLink.CheckedChanged += new System.EventHandler(this.chkDesktopLink_CheckedChanged);
//
// chkRunOnStartup
//
this.chkRunOnStartup.Checked = true;
this.chkRunOnStartup.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkRunOnStartup.Location = new System.Drawing.Point(8, 40);
this.chkRunOnStartup.Name = "chkRunOnStartup";
this.chkRunOnStartup.Size = new System.Drawing.Size(176, 24);
this.chkRunOnStartup.TabIndex = 2;
this.chkRunOnStartup.Text = "Start Automatically at Login";
this.chkRunOnStartup.CheckedChanged += new System.EventHandler(this.chkRunOnStartup_CheckedChanged);
//
// chkQuickLaunch
//
this.chkQuickLaunch.Checked = true;
this.chkQuickLaunch.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkQuickLaunch.Location = new System.Drawing.Point(8, 16);
this.chkQuickLaunch.Name = "chkQuickLaunch";
this.chkQuickLaunch.Size = new System.Drawing.Size(176, 24);
this.chkQuickLaunch.TabIndex = 1;
this.chkQuickLaunch.Text = "Show in Quick Launch Area";
this.chkQuickLaunch.CheckedChanged += new System.EventHandler(this.chkQuickLaunch_CheckedChanged);
//
// BtnExit
//
this.BtnExit.BackColor = System.Drawing.SystemColors.Control;
this.BtnExit.Cursor = System.Windows.Forms.Cursors.Arrow;
this.BtnExit.DialogResult = System.Windows.Forms.DialogResult.Abort;
this.BtnExit.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.BtnExit.Location = new System.Drawing.Point(192, 48);
this.BtnExit.Name = "BtnExit";
this.BtnExit.Size = new System.Drawing.Size(48, 23);
this.BtnExit.TabIndex = 11;
this.BtnExit.Text = "E&xit";
this.BtnExit.Click += new System.EventHandler(this.BtnExit_Click);
//
// chkSendToLink
//
this.chkSendToLink.Checked = true;
this.chkSendToLink.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkSendToLink.Location = new System.Drawing.Point(8, 88);
this.chkSendToLink.Name = "chkSendToLink";
this.chkSendToLink.Size = new System.Drawing.Size(160, 24);
this.chkSendToLink.TabIndex = 12;
this.chkSendToLink.Text = "Add to SendTo Menu";
this.chkSendToLink.CheckedChanged += new System.EventHandler(this.chkSendToLink_CheckedChanged);
//
// MainForm
//
this.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.BtnExit;
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(258, 127);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.chkSendToLink,
this.BtnExit,
this.chkDesktopLink,
this.chkRunOnStartup,
this.chkQuickLaunch});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MainForm";
this.Text = "Demonstrate Shell Links";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
public void BtnExit_Click(object sender, System.EventArgs e)
{
Exit();
}
/// <summary>
/// This method checks the startup directory to see if there is a link to the executable file
/// it modifies the directory accordingly depending on the setting of the RunOnStartup checkbox
/// </summary>
private void chkRunOnStartup_CheckedChanged(object sender, System.EventArgs e)
{
if(Skip)return;
Link.Update(Environment.SpecialFolder.Startup,Application.ExecutablePath,"Shell Link",chkRunOnStartup.Checked);
}
// Update a link to the executable on the desktop depending on the setting of chkDesktopLink
private void chkDesktopLink_CheckedChanged(object sender, System.EventArgs e)
{
if(Skip)return;
Link.Update(Environment.SpecialFolder.DesktopDirectory,Application.ExecutablePath,"Shell Link",chkDesktopLink.Checked);
}
演示怎样操作右键菜单,左面快捷方式,开始菜单
namespace ShellLinks
{
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);
}
}
}
有时在一些程序中使用右键时,所弹出的右键菜单中一些命令用英文显示,如果英文不好就会“一头雾水”。其实,它们是可以汉化的。比如安装了网络蚂蚁后,当用户浏览网页时如果单击鼠标右键,会发现在菜单中多了“Download All by NetAnts”,感到它和其他的中文选项有点不谐调。汉化它的办法是:打开注册表编辑器,定位到“HKEY_CURRENT_USER\Soft-ware\Microsoft\Internet Explorer\MenuExt\Download &&All by NetAnts”,再用鼠标右键单击“Download &&All by NetAnts”子键,在出现的快捷菜单中选择“重命名”,则“Download &&All by NetAnts”子键即变为可编辑状态,此时可将其修改为“全部由网络蚂蚁下载”,回车后,重新启动浏览器,汉化内容生效