Skip to content

Commit

Permalink
調整訊息框、錯誤處理
Browse files Browse the repository at this point in the history
  • Loading branch information
kouji6309 committed Apr 26, 2022
1 parent 41a72db commit 99bab9a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
51 changes: 24 additions & 27 deletions MainForm.Designer.cs

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

21 changes: 15 additions & 6 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void capacityBox_KeyUp(object sender, KeyEventArgs e) {

if (!Program.TryParseCapacity(capacityBox.Text, out var value)) {
capacityBox.ForeColor = Color.Red;
capacityToolTip.SetToolTip(capacityBox, "The capacity vaue is not valid.");
capacityToolTip.SetToolTip(capacityBox, "The capacity vaue is invalid.");
capacity = -1;
return;
}
Expand All @@ -104,7 +104,11 @@ private void capacityBox_KeyUp(object sender, KeyEventArgs e) {
private String _filename = "";
public String Filename {
get {
return Path.Combine(_basepath, _filename);
var filename = _filename;
if (!filename.ToLower().EndsWith(".vhdx")) {
filename = Path.ChangeExtension(filename, ".vhdx");
}
return Path.Combine(_basepath, filename);
}
set {
try {
Expand All @@ -130,7 +134,7 @@ private void filenameBox_KeyUp(object sender, KeyEventArgs e) {

if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) > 0) {
filenameBox.ForeColor = Color.Red;
filenameToolTip.SetToolTip(filenameBox, "The file name is not valid.");
filenameToolTip.SetToolTip(filenameBox, "The file name is invalid.");
return;
}

Expand Down Expand Up @@ -159,10 +163,15 @@ private void actionButton_Click(object sender, EventArgs e) {
command.Add("--capacity");
command.Add(Capacity.ToString());

var result = Program.RunProcess(Program.Location, command);
if (result != 0) {
MessageBox.Show(result.ToString(), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
try {
var result = Program.RunProcess(Program.Location, command);
if (result != 0) {
Program.Msgbox("Unable to create VHDX.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
} catch (Exception ex) {
Program.Msgbox(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

Close();
}
}
Expand Down
36 changes: 24 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ internal static class Program {

[STAThread]
private static Int32 Main(String[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Attached = AttachConsole(-1);
var errorCode = 0;
var message = "";
try {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var command = args.FirstOrDefault()?.ToLower();
if (command == "create") {
DoCreate(args);
Expand All @@ -67,7 +67,7 @@ private static Int32 Main(String[] args) {

if (!String.IsNullOrEmpty(message)) {
if (!Attached) {
MessageBox.Show(message, Title, MessageBoxButtons.OK, errorCode == 0 ? MessageBoxIcon.Information : MessageBoxIcon.Exclamation);
Msgbox(message, MessageBoxButtons.OK, errorCode == 0 ? MessageBoxIcon.Information : MessageBoxIcon.Exclamation);
} else {
Console.WriteLine("");
Console.WriteLine(message);
Expand Down Expand Up @@ -114,7 +114,7 @@ private static void DoCreate(String[] args) {
}

if (!IsPathValid(file)) {
throw new CvhdxXException("The file name is not valid.");
throw new CvhdxXException("The file name is invalid.");
}

if (File.Exists(file)) {
Expand Down Expand Up @@ -162,7 +162,7 @@ private static void DoExpand(String[] args) {
}

if (!IsPathValid(file)) {
throw new CvhdxXException("The file name is not valid.");
throw new CvhdxXException("The file name is invalid.");
}

if (!File.Exists(file)) {
Expand Down Expand Up @@ -198,7 +198,11 @@ private static Boolean DoCompact(String[] args) {
}

if (!IsElevated) {
RunProcess(Location, args);
try {
RunProcess(Location, args);
} catch (Exception ex) {
Msgbox(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return false;
}

Expand All @@ -207,7 +211,7 @@ private static Boolean DoCompact(String[] args) {
}

if (!IsPathValid(file)) {
throw new CvhdxXException("The file name is not valid.");
throw new CvhdxXException("The file name is invalid.");
}

if (!File.Exists(file)) {
Expand Down Expand Up @@ -235,7 +239,11 @@ private static void ShowForm(String[] args) {
var filename = args[0];
var fileExists = File.Exists(filename);
if (fileExists && !IsElevated) {
RunProcess(Location, args);
try {
RunProcess(Location, args);
} catch (Exception ex) {
Msgbox(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return;
}

Expand All @@ -251,14 +259,14 @@ private static void ShowForm(String[] args) {

Application.Run(form);
} catch (Exception ex) {
MessageBox.Show(ex.Message, Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Msgbox(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw;
}
}

private static void DoRegister() {
if (!IsElevated) {
MessageBox.Show("Run as administrator to register.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Msgbox("Run as administrator to register.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw new CvhdxXException("Access is denied.");
}

Expand Down Expand Up @@ -324,7 +332,7 @@ public static Int32 RunProcess(String filename, IEnumerable<String> args) {
p.WaitForExit();
return p.ExitCode;
} catch {
return -1;
throw;
}
}

Expand Down Expand Up @@ -373,6 +381,10 @@ public static Boolean TryParseCapacity(String str, out Int32 capacity) {
capacity = (Int32)(value * UnitMapping[matches[0].Groups[2].Value] / UnitMapping["MB"]);
return true;
}

public static DialogResult Msgbox(String text, MessageBoxButtons button, MessageBoxIcon icon) {
return MessageBox.Show(text, Title, button, icon);
}
}

internal class CvhdxXException : Exception {
Expand Down

0 comments on commit 99bab9a

Please sign in to comment.