Našiel som toto, je to projekt vo Visual Studio.
Form1
namespace StartupFolderShortcut
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
CreateStartupFolderShortcut();
}
private void buttonDelete_Click(object sender, EventArgs e)
{
DeleteStartupFolderShortcuts(Path.GetFileName(Application.ExecutablePath)) ;
}
public void CreateStartupFolderShortcut()
{
WshShellClass wshShell = new WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut;
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
// Create the shortcut
shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(startUpFolderPath + "\\" + Application.ProductName + ".lnk");
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Description = "Launch My Application";
// shortcut.IconLocation = Application.StartupPath + @"\App.ico";
shortcut.Save();
}
public string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = Path.GetDirectoryName(shortcutFilename);
string filenameOnly = Path.GetFileName(shortcutFilename);
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(pathOnly);
Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
return String.Empty; // Not found
}
public void DeleteStartupFolderShortcuts(string targetExeName)
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
DirectoryInfo di = new DirectoryInfo(startUpFolderPath);
FileInfo[] files = di.GetFiles("*.lnk");
foreach (FileInfo fi in files)
{
string shortcutTargetFile = GetShortcutTargetFile(fi.FullName);
Console.WriteLine("{0} -> {1}", fi.Name, shortcutTargetFile);
if (shortcutTargetFile.EndsWith(targetExeName, StringComparison.InvariantCultureIgnoreCase))
{
System.IO.File.Delete(fi.FullName);
}
}
}
}
}
Form1.Designer
namespace StartupFolderShortcut
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonDelete = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonCreate
//
this.buttonCreate.Location = new System.Drawing.Point(12, 12);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(131, 42);
this.buttonCreate.TabIndex = 0;
this.buttonCreate.Text = "Create Shortcut";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(12, 69);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(131, 42);
this.buttonDelete.TabIndex = 1;
this.buttonDelete.Text = "Delete Shortcuts";
this.buttonDelete.UseVisualStyleBackColor = true;
this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(160, 129);
this.Controls.Add(this.buttonDelete);
this.Controls.Add(this.buttonCreate);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button buttonCreate;
private System.Windows.Forms.Button buttonDelete;
}
}
Tento projekt je bez chyby aj všetko funguje, len ak tieto kódy hodím do 2. projektu tak vyhodí chybu a ani funkcia 1. v tom 2. nejde.
error CS0104: 'File' is an ambiguous reference between 'System.IO.File' and 'IWshRuntimeLibrary.File'
1. Ako mám spraviť aby mohli byť 2 File v jednej tej zložke? (1. File patrí k niečomu inému a 2. k tomuto.)
2. Ako by sa dalo tieto kódy prerobiť aby to fungovalo cez CheckBox? (odškrtnuté=Create a prázdne=Delete) Ďakujem.