Skip to content

Commit

Permalink
auto start
Browse files Browse the repository at this point in the history
  • Loading branch information
cyk2018 committed May 10, 2022
1 parent 0b29b86 commit 30e0275
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 33 deletions.
95 changes: 62 additions & 33 deletions Reminder/MainFrm.Designer.cs

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

109 changes: 109 additions & 0 deletions Reminder/MainFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using IWshRuntimeLibrary;

namespace Reminder
{
Expand Down Expand Up @@ -35,6 +38,10 @@ private void Btn_start_Click(object sender, EventArgs e)
input_flag = false;
}

bool auto_start;
auto_start = checkBox1.Checked;
SetAutoStart(auto_start);

int wrkTime = (int)this.numWrkTime.Value;
int rstTime = (int)this.numRstTime.Value;
wrkFrm = new WorkFrm(wrkTime,rstTime,input_flag);
Expand Down Expand Up @@ -78,5 +85,107 @@ private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
AboutBox aboutBox = new AboutBox();
aboutBox.ShowDialog();
}

private string QuickName = "Reminder";

private string systemStartPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.Startup); } }

private string appAllPath { get { return Process.GetCurrentProcess().MainModule.FileName; } }

private void SetAutoStart(bool auto_run)
{
// get the path set of this software
List<string> shortcurPaths = GetQuickFromFolder(systemStartPath, appAllPath);
if (auto_run)
{
if (shortcurPaths.Count > 1)
{
for (int i = 1; i < shortcurPaths.Count; i++)
{
DeleteFile(shortcurPaths[i]);
}
}
else if (shortcurPaths.Count == 0)
{
bool res = CreateShortcut(systemStartPath, QuickName, appAllPath, "Reminder");
}
}
else
{
if (shortcurPaths.Count > 0)
{
for (int i = 0; i < shortcurPaths.Count; i++)
{
DeleteFile(shortcurPaths[i]);
}
}
}
}

private bool CreateShortcut(string systemStartPath, string quickName, string appAllPath, string description = null)
{
try
{
if (!Directory.Exists(systemStartPath)) Directory.CreateDirectory(systemStartPath);
string shortcutPath = Path.Combine(systemStartPath, string.Format("{0}.lnk", quickName));
WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = appAllPath;
shortcut.WorkingDirectory = Path.GetDirectoryName(shortcutPath);
shortcut.WindowStyle = 1;
shortcut.Description = description;
shortcut.IconLocation = "ICO2.ico";
shortcut.Save();
return true;
}
catch (Exception ex)
{
string message = ex.Message;
}
return false;
}

private void DeleteFile(string path)
{
FileAttributes attr = System.IO.File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
Directory.Delete(path, true);
}
else
{
System.IO.File.Delete(path);
}
}

private List<string> GetQuickFromFolder(string systemStartPath, string appAllPath)
{
List<string> tmpStrs = new List<string>();
String tmpStr = null;
String[] files = Directory.GetFiles(systemStartPath, "*.lnk");
for (int i = 0; i < files.Length; i++)
{
tmpStr = GetAppPathFromQuick(files[i]);
if (tmpStr == appAllPath)
{
tmpStrs.Add(files[i]);
}
}
return tmpStrs;
}

private string GetAppPathFromQuick(string shortcutPath)
{
if (System.IO.File.Exists(shortcutPath))
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
return shortcut.TargetPath;
}
else
{
return null;
}
}
}
}

0 comments on commit 30e0275

Please sign in to comment.