Skip to content

Commit

Permalink
Merge branch 'release/0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwinning committed May 31, 2016
2 parents 4eac8d2 + 9de9bc1 commit f38b366
Show file tree
Hide file tree
Showing 13 changed files with 1,655 additions and 0 deletions.
6 changes: 6 additions & 0 deletions App.config
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>
39 changes: 39 additions & 0 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions Form1.cs
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());
}
}

}
}
Loading

0 comments on commit f38b366

Please sign in to comment.