-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMainForm.cs
More file actions
103 lines (87 loc) · 3.4 KB
/
MainForm.cs
File metadata and controls
103 lines (87 loc) · 3.4 KB
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
103
using System.Runtime.InteropServices;
using WindowsShortcutFactory;
using File = System.IO.File;
namespace TaskSplitter11
{
public partial class MainForm : Form
{
public enum ShowWindowCommands
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_MAX = 10
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr ShellExecute(
IntPtr hwnd,
[MarshalAs(UnmanagedType.LPWStr)] string lpszOp,
[MarshalAs(UnmanagedType.LPWStr)] string lpszFile,
[MarshalAs(UnmanagedType.LPWStr)] string lpszParams,
[MarshalAs(UnmanagedType.LPWStr)] string lpszDir,
ShowWindowCommands FsShowCmd
);
public MainForm()
{
InitializeComponent();
}
private void BtnCreate_Click(object sender, EventArgs e)
{
//Setup & config
string? appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string? appPath = Path.GetDirectoryName(Application.ExecutablePath);
string? shortcutsPath = Path.Join(appDataPath, "TaskSeparator11", "Shortcuts");
Directory.CreateDirectory(shortcutsPath);
//Ensure files are in thwe right place
EnsureSplitterFilesAreAvailable(appPath, shortcutsPath);
//Make a copy of the Splitter exe
string splitterExe = Path.Join(appPath, "Splitter.exe");
string splitterExeLocation = GetNewLinkName(shortcutsPath, "exe");
File.Copy(splitterExe, splitterExeLocation);
//Create a shortcut to the Splitter exe
string linkLocation = GetNewLinkName(shortcutsPath, "lnk");
//Create shortcut
using var shortcut = new WindowsShortcut
{
Path = splitterExeLocation,
};
shortcut.Save(linkLocation);
ShellExecute(IntPtr.Zero, "open", linkLocation, "--gui", "", ShowWindowCommands.SW_NORMAL);
Application.Exit();
}
private static void EnsureSplitterFilesAreAvailable(string? appPath, string shortcutsPath)
{
if (File.Exists(Path.Join(shortcutsPath, "Splitter.dll")) || string.IsNullOrWhiteSpace(appPath)) return;
var dir = new DirectoryInfo(appPath);
var files = dir.GetFiles("Splitter*");
files.Select(f => f.Name).ToList().ForEach(fileName =>
{
var source = Path.Join(appPath, fileName);
var dest = Path.Join(shortcutsPath, fileName);
File.Copy(source, dest, true);
});
}
private static string GetNewLinkName(string path, string ext)
{
int count = 1;
char c = ext == "exe" ? '_' : ' ';
string shortcutLink;
do
{
shortcutLink = Path.Join(path, $"{new string(c, count)}.{ext}");
count++;
} while (File.Exists(shortcutLink));
return shortcutLink;
}
}
}