-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
102 lines (95 loc) · 3.5 KB
/
Form1.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
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IWshRuntimeLibrary; //For creating shortcuts (Add References > COM > Windows Script Host Object)
namespace Windows_Add_To_Start
{
public partial class Form1 : Form
{
/// <summary>
/// Setting default form visibility to hidden on load.
/// Form is only set to be visible when user input is required.
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
public Form1()
{
InitializeComponent();
string[] args = Environment.GetCommandLineArgs();
//MessageBox.Show(args[1]);
CopyToStartMenu(args[1]);
//close the form
Close();
}
/// <summary>
/// Takes the filepath.
/// Copies it to the start menu if the file is a shortcut.
/// Creates a shortcut in the start menu for all other file types.
/// </summary>
/// <param name="path"></param>
void CopyToStartMenu(string path)
{
//Get win root
string windowsDrive = Path.GetPathRoot(Environment.SystemDirectory);
//path to start menu
string startMenuPath = "ProgramData\\Microsoft\\Windows\\Start Menu\\Programs";
//Get filename from path
string fileName = Path.GetFileNameWithoutExtension(path);
//Get extension
string extension = Path.GetExtension(path);
//If the path is not a shortcut
if (extension != ".lnk" && extension != ".url" && extension != ".appref-ms")
{
CreateShortcut(fileName, windowsDrive + startMenuPath, path);
}
else //If the path is a shortcut
{
try
{
System.IO.File.Copy(path, windowsDrive + startMenuPath + Path.DirectorySeparatorChar + fileName + extension);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//return;
}
/// <summary>
/// Creates a shortcut.
/// </summary>
/// <param name="shortcutName"></param>
/// <param name="shortcutPath"></param>
/// <param name="targetFileLocation"></param>
public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation)
{
string shortcutLocation = System.IO.Path.Combine(shortcutPath, shortcutName + ".lnk");
MessageBox.Show(shortcutLocation);
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.WorkingDirectory = Application.StartupPath; //?
//shortcut.IconLocation = @"c:\myicon.ico"; // The icon of the shortcut
shortcut.TargetPath = targetFileLocation; // The path of the file that will launch when the shortcut is run
try
{
shortcut.Save();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}