Skip to content

Commit 9ab7c36

Browse files
committed
Clear code
1 parent 9e89694 commit 9ab7c36

3 files changed

Lines changed: 63 additions & 40 deletions

File tree

Program.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5-
using System.Drawing;
6-
using System.IO;
75
using System.Linq;
86
using System.Text.RegularExpressions;
97
using System.Windows.Forms;
@@ -21,62 +19,65 @@ public static void Main()
2119

2220
}
2321

24-
private NotifyIcon trayIcon;
25-
private ContextMenu trayMenu;
26-
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
27-
private string appName = "WinToLinux";
22+
private readonly NotifyIcon trayIcon;
23+
private readonly ContextMenu trayMenu;
24+
readonly RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
25+
private readonly string appName = "WinToLinux";
2826

2927
List<string> uefi = new List<string>();
3028
List<string> uuid = new List<string>();
3129
string bootsequence;
3230
string currentValue;
3331
int shift;
34-
35-
Regex regexUUID = new Regex("^(\\{){0,1}[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}(\\}){0,1}$");
32+
readonly Regex regexUUID = new Regex("^(\\{){0,1}[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}(\\}){0,1}$");
3633

3734
public SysTrayApp()
3835
{
3936
bool isStart = registryKey.GetValue(appName) != null;
40-
getMenuItems();
37+
GetMenuItems();
4138

42-
currentValue = bootsequence != null ? bootsequence : uuid.First();
39+
currentValue = bootsequence ?? uuid.First();
4340
shift = uuid.Count() - uefi.Count();
4441

4542
// Create a simple tray menu with only one item.
4643
trayMenu = new ContextMenu();
4744
trayMenu.MenuItems.Add("Settings").Enabled = false;
4845
trayMenu.MenuItems.Add("-");
49-
trayMenu.MenuItems.Add("Start with system", onRegisterInStartup).Checked = isStart;
46+
trayMenu.MenuItems.Add("Start with system", OnRegisterInStartup).Checked = isStart;
5047
trayMenu.MenuItems.Add("-");
5148
trayMenu.MenuItems.Add("Reboot to...").Enabled = false;
5249
trayMenu.MenuItems.Add("-");
5350

5451
foreach (var pos in uefi.Select((value, i) => new { i, value }))
52+
{
53+
MenuItem item = new MenuItem
5554
{
56-
MenuItem item = new MenuItem();
57-
item.Checked = uuid[pos.i + shift] == currentValue;
58-
item.Tag = uuid[pos.i + shift];
59-
item.Text = pos.value;
60-
item.Click += onMenuClick;
55+
Checked = uuid[pos.i + shift] == currentValue,
56+
Tag = uuid[pos.i + shift],
57+
Text = pos.value
58+
};
59+
item.Click += OnMenuClick;
6160
trayMenu.MenuItems.Add(item);
6261
}
6362

6463
trayMenu.MenuItems.Add("-");
65-
trayMenu.MenuItems.Add("Reboot system", onReboot);
64+
trayMenu.MenuItems.Add("Reboot system", OnReboot);
6665
trayMenu.MenuItems.Add("-");
6766
trayMenu.MenuItems.Add("Exit", OnExit);
6867

6968
// Create a tray icon. In this example we use a
7069
// standard system icon for simplicity, but you
7170
// can of course use your own custom icon too.
7271

73-
trayIcon = new NotifyIcon();
74-
trayIcon.Text = "Reboot to Linux";
75-
trayIcon.Icon = WinToLinux.Properties.Resources.WtL;
72+
trayIcon = new NotifyIcon
73+
{
74+
Text = "Reboot to Linux",
75+
Icon = WinToLinux.Properties.Resources.WtL,
7676

77-
// Add menu to tray icon and show it.
78-
trayIcon.ContextMenu = trayMenu;
79-
trayIcon.Visible = true;
77+
// Add menu to tray icon and show it.
78+
ContextMenu = trayMenu,
79+
Visible = true
80+
};
8081
}
8182

8283

@@ -85,28 +86,28 @@ private void OnExit(object sender, EventArgs e)
8586
Application.Exit();
8687
}
8788

88-
private void onMenuClick(object sender, EventArgs e)
89+
private void OnMenuClick(object sender, EventArgs e)
8990
{
9091
string UUID = (sender as MenuItem).Tag.ToString();
9192
string command = "/C bcdedit.exe /set {fwbootmgr} bootsequence " + UUID + " /addfirst";
9293
Console.WriteLine(command);
9394

94-
launchCMD(command);
95+
LaunchCMD(command);
9596

9697
uuid = new List<string>();
9798
uefi = new List<string>();
9899

99-
launchCMD("/C bcdedit /enum firmware");
100-
101-
currentValue = bootsequence != null ? bootsequence : uuid.First();
100+
LaunchCMD("/C bcdedit /enum firmware");
101+
102+
currentValue = bootsequence ?? uuid.First();
102103
shift = uuid.Count() - uefi.Count();
103104
foreach (var pos in uefi.Select((value, i) => new { i, value }))
104105
{
105106
trayMenu.MenuItems[pos.i + 6].Checked = uuid[pos.i + shift] == currentValue;
106107
}
107108
}
108109

109-
private void onRegisterInStartup(object sender, EventArgs e)
110+
private void OnRegisterInStartup(object sender, EventArgs e)
110111
{
111112
bool isStartup = registryKey.GetValue(appName) == null;
112113

@@ -122,17 +123,17 @@ private void onRegisterInStartup(object sender, EventArgs e)
122123
}
123124
}
124125

125-
private void onReboot(object sender, EventArgs e)
126+
private void OnReboot(object sender, EventArgs e)
126127
{
127-
launchCMD("/C shutdown /r /t 0");
128+
LaunchCMD("/C shutdown /r /t 0");
128129
}
129130

130-
private void getMenuItems()
131+
private void GetMenuItems()
131132
{
132-
launchCMD("/C bcdedit /enum firmware");
133+
LaunchCMD("/C bcdedit /enum firmware");
133134
}
134135

135-
private void launchCMD(string command)
136+
private void LaunchCMD(string command)
136137
{
137138
Process build = new Process();
138139
build.StartInfo.Arguments = command;
@@ -142,16 +143,16 @@ private void launchCMD(string command)
142143
build.StartInfo.RedirectStandardOutput = true;
143144
build.StartInfo.RedirectStandardError = true;
144145
build.StartInfo.CreateNoWindow = true;
145-
build.ErrorDataReceived += build_ErrorAndDataReceived;
146-
build.OutputDataReceived += build_ErrorAndDataReceived;
146+
build.ErrorDataReceived += Build_ErrorAndDataReceived;
147+
build.OutputDataReceived += Build_ErrorAndDataReceived;
147148
build.EnableRaisingEvents = true;
148149
build.Start();
149150
build.BeginOutputReadLine();
150151
build.BeginErrorReadLine();
151152
build.WaitForExit();
152153
}
153154

154-
void build_ErrorAndDataReceived(object sender, DataReceivedEventArgs e)
155+
void Build_ErrorAndDataReceived(object sender, DataReceivedEventArgs e)
155156
{
156157
if (e.Data != null && e.Data != "")
157158
{
@@ -163,7 +164,7 @@ void build_ErrorAndDataReceived(object sender, DataReceivedEventArgs e)
163164
if (splited[0] == "description")
164165
{
165166
splited = splited.Where(w => w != splited[0]).ToArray();
166-
Console.WriteLine(String.Join(" ",splited));
167+
Console.WriteLine(String.Join(" ", splited));
167168
uefi.Add(String.Join(" ", splited));
168169
}
169170

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
// набора атрибутов. Измените значения этих атрибутов для изменения сведений,
77
// связанных со сборкой.
88
[assembly: AssemblyTitle("WinToLinux")]
9-
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyDescription("Reboot to Linux easier than ever")]
1010
[assembly: AssemblyConfiguration("")]
1111
[assembly: AssemblyCompany("")]
1212
[assembly: AssemblyProduct("WinToLinux")]
13-
[assembly: AssemblyCopyright("Copyright © 2020")]
13+
[assembly: AssemblyCopyright("Anton Palgunov. Copyright © 2020")]
1414
[assembly: AssemblyTrademark("")]
1515
[assembly: AssemblyCulture("")]
1616

WinToLinux.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
<FileAlignment>512</FileAlignment>
1313
<Deterministic>true</Deterministic>
1414
<TargetFrameworkProfile />
15+
<PublishUrl>publish\</PublishUrl>
16+
<Install>true</Install>
17+
<InstallFrom>Disk</InstallFrom>
18+
<UpdateEnabled>false</UpdateEnabled>
19+
<UpdateMode>Foreground</UpdateMode>
20+
<UpdateInterval>7</UpdateInterval>
21+
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
22+
<UpdatePeriodically>false</UpdatePeriodically>
23+
<UpdateRequired>false</UpdateRequired>
24+
<MapFileExtensions>true</MapFileExtensions>
25+
<ApplicationRevision>0</ApplicationRevision>
26+
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
27+
<IsWebBootstrapper>false</IsWebBootstrapper>
28+
<UseApplicationTrust>false</UseApplicationTrust>
29+
<BootstrapperEnabled>true</BootstrapperEnabled>
1530
</PropertyGroup>
1631
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1732
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -79,5 +94,12 @@
7994
<ItemGroup>
8095
<Content Include="WtL.ico" />
8196
</ItemGroup>
97+
<ItemGroup>
98+
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
99+
<Visible>False</Visible>
100+
<ProductName>.NET Framework 3.5 SP1</ProductName>
101+
<Install>false</Install>
102+
</BootstrapperPackage>
103+
</ItemGroup>
82104
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
83105
</Project>

0 commit comments

Comments
 (0)