Skip to content

Commit

Permalink
修复 bug, 增加异常处理
Browse files Browse the repository at this point in the history
更新版本号为 v2.0.1
  • Loading branch information
HumXC committed May 29, 2022
1 parent 359a1c6 commit e0b9bcb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion AutoVDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<StartupObject>AutoVDesktop.Program</StartupObject>
<ApplicationIcon>Icon.ico</ApplicationIcon>
<DebugType>embedded</DebugType>
<Version>2.0.0</Version>
<Version>2.0.1</Version>
<PackageLicenseFile>D:\Desktops\Code\DEV\AutoVDesktop\LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Authors>HumXC</Authors>
Expand Down
12 changes: 9 additions & 3 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ internal class Config
public bool ShowNotifyIcon { get; set; } = true;
public bool DebugMode { get; set; } = false;
public bool StartWithWindows { get; set; } = false;
public readonly string confFileName = Path.Combine(Environment.CurrentDirectory, "config.json");


public readonly string confFileName;
public Config()
{
var path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
if (path == null)
{
throw new Exception("无法读取程序运行目录");
}
confFileName = Path.Combine(path, "config.json");

// 检查文件夹名的合法性
foreach (var desktopName in Desktops)
{
Expand Down Expand Up @@ -50,6 +55,7 @@ public void LoadConfig()
this.Delay = c.Delay;
this.StartWithWindows = c.StartWithWindows;
this.Desktops = c.Desktops;
this.EnsureRestore = c.EnsureRestore;
}

}
Expand Down
34 changes: 16 additions & 18 deletions DesktopRestorer/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@ public static void SaveIconsAndRegistry(IEnumerable<NamedDesktopPoint> iconPosit
registryValues.Select(p => new XElement("Value",
new XElement("Name", new XCData(p.Key)),
new XElement("Data", new XCData(p.Value)))))));
string filePath = Path.Combine(Environment.CurrentDirectory, "Desktops", fileName + ".xml");
if (File.Exists(filePath))
if (File.Exists(fileName))
{
File.Delete(filePath);
File.Delete(fileName);
}
else if (!Directory.Exists(Path.GetDirectoryName(filePath)))
var dir = Path.GetDirectoryName(fileName);
if (dir == null)
{
var dirName = Path.GetDirectoryName(filePath);
if (dirName != null)
{
Directory.CreateDirectory(dirName);
}


throw new Exception("找不到文件的父目录: \n" + fileName);
}
using Stream outStream = File.OpenWrite(filePath);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
using Stream outStream = File.OpenWrite(fileName);
using var writer = XmlWriter.Create(outStream);
xDoc.WriteTo(writer);

Expand All @@ -53,14 +48,17 @@ public static void SaveIconPositions(IEnumerable<NamedDesktopPoint> iconPosition
new XAttribute("x", p.X),
new XAttribute("y", p.Y),
new XText(p.Name))))));
string fileDir = Path.Combine(Environment.CurrentDirectory, "Desktops");
string filePath = Path.Combine(fileDir, fileName + ".xml");
if (File.Exists(filePath))
if (File.Exists(fileName))
{
File.Delete(fileName);
}
var dir = Path.GetDirectoryName(fileName);
if (dir == null)
{
File.Delete(filePath);
throw new Exception("找不到文件的父目录: \n" + fileName);
}
else if (!Directory.Exists(filePath)) Directory.CreateDirectory(fileDir);
using Stream outStream = File.OpenWrite(filePath);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
using Stream outStream = File.OpenWrite(fileName);
using var writer = XmlWriter.Create(outStream);
xDoc.WriteTo(writer);

Expand Down
8 changes: 6 additions & 2 deletions OptionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void SaveConfig_Click(object sender, EventArgs e)

//开机自启
Microsoft.Win32.RegistryKey RKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
var appName = Environment.ProcessPath;
var appName = System.Windows.Forms.Application.ExecutablePath;
if (appName != null)
{
var k = RKey.GetValue("AutoVDesktop");
Expand All @@ -155,7 +155,11 @@ private void SaveConfig_Click(object sender, EventArgs e)
}
}
}

else
{
MessageBox.Show("错误", "设置自启动失败, 程序名为 null 。", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Program.config.DebugMode)
{
this.Visible = false;
Expand Down
34 changes: 28 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ internal static class Program
private static readonly object lockObj = new();
private static int threadID = 0;

private static readonly string dataPath = Path.Combine(Environment.CurrentDirectory, "Desktops");
private static readonly string dataPath;
static Program()
{
var path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
if (path == null)
{
MessageBox.Show("错误", "无法读取程序运行目录", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
dataPath = Path.Combine(path, "Desktops");
}
[STAThread]
static void Main()
{
Expand All @@ -30,15 +40,26 @@ static void Main()
VirtualDesktop.VirtualDesktop.CurrentChanged += (oldDesktop, newDesktop) =>
{
Logger.Debug($"线程{threadID}: 切换桌面: {oldDesktop.Name} -> {newDesktop.Name}");
ThreadPool.QueueUserWorkItem((state) => { ChangeDesktop(newDesktop.Name, threadID); });
ThreadPool.QueueUserWorkItem((state) =>
{
try
{
ChangeDesktop(newDesktop.Name, threadID);
}
catch (Exception e)
{
MessageBox.Show("错误", "切换桌面时出现错误: \n" + e.Message + "\n" + e.StackTrace, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
});
++threadID;
};
Application.Run(new OptionView());
}
catch (Exception e)
{
MessageBox.Show("Error: \n" + e.Message + "\n" + e.StackTrace, "Error");
Process.GetCurrentProcess().Kill();
MessageBox.Show("错误", "错误: \n" + e.Message + "\n" + e.StackTrace, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}

}
Expand All @@ -54,6 +75,7 @@ static void ChangeDesktop(string newDesktopName, int _threadID)
{
Logger.Debug($"线程{_threadID}: 获得锁,开始运行...");
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Logger.Debug("Desktop_Path=" + path);
string nowDesktopName = Path.GetFileName(path);
string? desktopPath = Path.GetDirectoryName(path);
if (newDesktopName.Equals(nowDesktopName))
Expand Down Expand Up @@ -99,7 +121,7 @@ static DesktopRestorer.Desktop SaveDesktop(string desktopName)
var desktop = new DesktopRestorer.Desktop();
var iconPositions = desktop.GetIconsPositions();
Program.Logger.Debug("开始保存桌面图标位置: " + desktopName);
Storage.SaveIconPositions(iconPositions, desktopName);
Storage.SaveIconPositions(iconPositions, Path.Combine(dataPath, desktopName + ".xml"));
return desktop;
}
static DesktopRestorer.Desktop SetDesktop(string desktopName)
Expand Down Expand Up @@ -131,7 +153,7 @@ static void InitConf(Config config)
MessageBox.Show("错误", "设置自启动失败, 无法打开注册表。", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var appName = Environment.ProcessPath;
var appName = System.Windows.Forms.Application.ExecutablePath;
if (appName == null)
{
MessageBox.Show("错误", "设置自启动失败, 程序名为 null 。", MessageBoxButtons.OK, MessageBoxIcon.Error);
Expand Down

0 comments on commit e0b9bcb

Please sign in to comment.