Skip to content

Commit

Permalink
run at windows startup
Browse files Browse the repository at this point in the history
  • Loading branch information
BeloMaximka committed Apr 23, 2023
1 parent 7cb6095 commit d11e838
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MystatDesktopWpf/Languages/lang.en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,8 @@
<v:String x:Key="m_Clearing">Clearing...</v:String>
<v:String x:Key="m_Calculating">Calculating...</v:String>
<v:String x:Key="m_FailedToClear">Failed to clear!</v:String>

<!-- Windows settings -->
<v:String x:Key="m_WindowsSettings">Windows settings</v:String>
<v:String x:Key="m_RunOnWindowsStartup">Run on Windows startup</v:String>
</ResourceDictionary>
4 changes: 4 additions & 0 deletions MystatDesktopWpf/Languages/lang.ru-RU.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,8 @@
<v:String x:Key="m_Clearing">Очистка...</v:String>
<v:String x:Key="m_Calculating">Вычисление...</v:String>
<v:String x:Key="m_FailedToClear">Не получилось очистить кэш!</v:String>

<!-- Windows settings -->
<v:String x:Key="m_WindowsSettings">Настройки Windows</v:String>
<v:String x:Key="m_RunOnWindowsStartup">Запускать при старте Windows</v:String>
</ResourceDictionary>
4 changes: 4 additions & 0 deletions MystatDesktopWpf/Languages/lang.uk-UA.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,8 @@
<v:String x:Key="m_Clearing">Очистка...</v:String>
<v:String x:Key="m_Calculating">Розрахунок...</v:String>
<v:String x:Key="m_FailedToClear">Не вдалося очистити!</v:String>

<!-- Windows settings -->
<v:String x:Key="m_WindowsSettings">Налаштування Windows</v:String>
<v:String x:Key="m_RunOnWindowsStartup">Запускати під час старту Windows</v:String>
</ResourceDictionary>
12 changes: 12 additions & 0 deletions MystatDesktopWpf/MystatDesktopWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
<None Remove="Resources\trayicon.ico" />
</ItemGroup>

<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="MaterialDesignThemes" Version="4.6.1" />
Expand Down
1 change: 1 addition & 0 deletions MystatDesktopWpf/UserControls/Menus/Settings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel Margin="16">
<controls:WindowsSettings Margin="8"/>
<controls:NotificationSettings Margin="8"/>
<controls:ThemeSettings Margin="8"/>
<controls:TraySettings Margin="8"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<UserControl x:Class="MystatDesktopWpf.UserControls.SettingsSections.WindowsSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MystatDesktopWpf.UserControls.SettingsSections"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<materialDesign:Card Padding="8">
<StackPanel Margin="8 0 0 0">
<TextBlock Text="{DynamicResource m_WindowsSettings}" HorizontalAlignment="Left" Style="{DynamicResource MaterialDesignBody1TextBlock}"/>
<StackPanel Orientation="Horizontal" Margin="0 8">
<ToggleButton x:Name="StartupToggle" Unchecked="ToggleButton_Unchecked" />
<TextBlock Text="{DynamicResource m_RunOnWindowsStartup}" Margin="8 0 0 0"/>
</StackPanel>
</StackPanel>

</materialDesign:Card>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using IWshRuntimeLibrary;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace MystatDesktopWpf.UserControls.SettingsSections
{
/// <summary>
/// Interaction logic for WindowsSettings.xaml
/// </summary>
public partial class WindowsSettings : UserControl
{
public WindowsSettings()
{
InitializeComponent();
StartupToggle.IsChecked = System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\Mystat Desktop.lnk");
StartupToggle.Checked += ToggleButton_Checked;
}

// Create shortcut to startup folder
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
if (sender is ToggleButton toggleButton)
{
try
{
string appPath = Process.GetCurrentProcess().MainModule.FileName;
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\Mystat Desktop.lnk";

WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);

shortcut.TargetPath = appPath;
shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(appPath);
shortcut.Save();
}
catch (Exception)
{
toggleButton.IsChecked = false;
}
}
}

private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
if (sender is ToggleButton toggleButton)
{
try
{
System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\Mystat Desktop.lnk");
}
catch (Exception)
{
toggleButton.IsChecked = true;
}
}
}
}
}

0 comments on commit d11e838

Please sign in to comment.