Skip to content

Commit

Permalink
新增开机启动
Browse files Browse the repository at this point in the history
  • Loading branch information
luxin committed Jun 28, 2020
1 parent d7c3526 commit ebdfe52
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
9 changes: 9 additions & 0 deletions RecycleBin/Properties/ResourcesEn.Designer.cs

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

3 changes: 3 additions & 0 deletions RecycleBin/Properties/ResourcesEn.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@
<data name="SupportedLanguages" xml:space="preserve">
<value>简体中文,English</value>
</data>
<data name="AutoStart" xml:space="preserve">
<value>Auto start while boot</value>
</data>
</root>
9 changes: 9 additions & 0 deletions RecycleBin/Properties/ResourcesZhCN.Designer.cs

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

3 changes: 3 additions & 0 deletions RecycleBin/Properties/ResourcesZhCN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@
<data name="SupportedLanguages" xml:space="preserve">
<value>简体中文,English</value>
</data>
<data name="AutoStart" xml:space="preserve">
<value>开机自启</value>
</data>
</root>
1 change: 1 addition & 0 deletions RecycleBin/RecycleBin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<ApplicationIcon>Resources/recycle-bin.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
Expand Down
58 changes: 50 additions & 8 deletions RecycleBin/Tray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Tray
private bool _recycleBinIsEmpty = true;
private readonly RecycleBinHandle _recycleBinHandle;
private readonly Language _language = new Language();
private const string ExeName = "RecycleBin.exe";

public Tray()
{
Expand All @@ -29,14 +30,19 @@ public void CreateTrayIcon()
Visible = true
};

var autoStartMenuItem = new ToolStripMenuItem(_language.Get("AutoStart"), null, AutoStartHandler);
var languageMenuItem = new ToolStripMenuItem(_language.Get("Language"), null, (sender, e) => { });
var openMenuItem = new ToolStripMenuItem(_language.Get("OpenRecycleBin"), null, _recycleBinHandle.Open);
var clearMenuItem = new ToolStripMenuItem(_language.Get("ClearRecycleBin"), null, _recycleBinHandle.Clear);
var exitMenuItem = new ToolStripMenuItem(_language.Get("Exit"), null, TrayExit);

// 查看是否已经设置为开机启动
autoStartMenuItem.Checked = IsAutoStart();

// 添加菜单
var contextMenuStrip = new ContextMenuStrip();
// contextMenuStrip.Items.Add(settingsMenuItem);
contextMenuStrip.Items.Add(autoStartMenuItem);
contextMenuStrip.Items.Add(languageMenuItem);
contextMenuStrip.Items.Add(openMenuItem);
contextMenuStrip.Items.Add(clearMenuItem);
Expand Down Expand Up @@ -69,10 +75,11 @@ public void CreateTrayIcon()
dropDownItemTmp.Checked = false;
}

contextMenuStrip.Items[0].Text = _language.Get("Language");
contextMenuStrip.Items[1].Text = _language.Get("OpenRecycleBin");
contextMenuStrip.Items[2].Text = _language.Get("ClearRecycleBin");
contextMenuStrip.Items[3].Text = _language.Get("Exit");
contextMenuStrip.Items[0].Text = _language.Get("AutoStart");
contextMenuStrip.Items[1].Text = _language.Get("Language");
contextMenuStrip.Items[2].Text = _language.Get("OpenRecycleBin");
contextMenuStrip.Items[3].Text = _language.Get("ClearRecycleBin");
contextMenuStrip.Items[4].Text = _language.Get("Exit");
});

if (_language.GetLocal() == language) languageMenuItemTmp.Checked = true;
Expand Down Expand Up @@ -128,20 +135,37 @@ public void RemoveTrayIcon()
_notifyIcon = null;
}

public static bool CreateShortcut(string directory, string shortcutName, string targetPath,
string description = null, string iconLocation = null)
private static void AutoStartHandler(object sender, EventArgs e)
{
if (!(sender is ToolStripMenuItem menuItem)) return;

menuItem.Checked = !menuItem.Checked ? SetAutoStart() : !CancelAutoStart();
}

/// <summary>
/// 通过在用户开机
/// </summary>
/// <param name="description">快捷方式描述</param>
/// <param name="iconLocation">快捷方式图标</param>
/// <returns>如果设置开机启动成功,那么就返回true</returns>
private static bool SetAutoStart(string description = null, string iconLocation = null)
{
try
{
// 存放快捷方式文件的完整路径
// 获取所有用户的 开始 文件夹位置
var directory = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);

// 添加引用 Com 中搜索 Windows.Script.Host.Object.Model
var shortcutPath = Path.Combine(directory, $"{shortcutName}.lnk");
var shortcutPath = Path.Combine(directory, $"{ExeName}.lnk");
var shell = new IWshRuntimeLibrary.WshShell();
// 创建快捷方式对象
var shortcut = (IWshRuntimeLibrary.IWshShortcut) shell.CreateShortcut(shortcutPath);
// 当前程序完整路径
var targetPath = Directory.GetCurrentDirectory();
// 指定目标路径
shortcut.TargetPath = targetPath;
shortcut.TargetPath = $"{targetPath}\\{ExeName}";
// 设置起始位置
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
// 设置运行方式,默认为常规窗口
Expand All @@ -162,5 +186,23 @@ public static bool CreateShortcut(string directory, string shortcutName, string

return false;
}

private static bool IsAutoStart()
{
var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
return File.Exists($"{startupPath}\\{ExeName}.lnk");
}

/// <summary>
/// 取消开机启动
/// </summary>
/// <returns>如果取消开机启动成功,就返回true</returns>
private static bool CancelAutoStart()
{
var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
var path = $"{startupPath}\\{ExeName}.lnk";
File.Delete(path);
return !File.Exists(path);
}
}
}

0 comments on commit ebdfe52

Please sign in to comment.