-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,655 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> | ||
</startup> | ||
</configuration> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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()); | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.