OSDN Git Service

設定でファイル名に空白を入力して参照を押すとエラーになるのを直す
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Thu, 7 Jun 2018 11:43:55 +0000 (20:43 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Thu, 7 Jun 2018 11:43:55 +0000 (20:43 +0900)
KancolleSniffer/ConfigDialog.cs

index 4f8d1d6..721f7e8 100644 (file)
@@ -257,10 +257,7 @@ namespace KancolleSniffer
 \r
         private void buttonOpenFile_Click(object sender, EventArgs e)\r
         {\r
-            openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
-            openSoundFileDialog.InitialDirectory = String.IsNullOrEmpty(textBoxSoundFile.Text)\r
-                ? Config.BaseDir\r
-                : Path.GetDirectoryName(textBoxSoundFile.Text) ?? Config.BaseDir;\r
+            SetInitialPath(openSoundFileDialog, textBoxSoundFile.Text);\r
             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
                 return;\r
             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
@@ -333,13 +330,37 @@ namespace KancolleSniffer
 \r
         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
         {\r
-            openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
-            openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
+            SetInitialPath(openDebugLogDialog, textBoxDebugLog.Text);\r
             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
         }\r
 \r
+        private void SetInitialPath(OpenFileDialog dialog, string path)\r
+        {\r
+            var dir = Config.BaseDir;\r
+            var file = "";\r
+            if (!string.IsNullOrWhiteSpace(path))\r
+            {\r
+                var res = Path.GetDirectoryName(path);\r
+                if (res == null) // root\r
+                {\r
+                    dir = path;\r
+                }\r
+                else if (res != "") // contain directory\r
+                {\r
+                    dir = res;\r
+                    file = Path.GetFileName(path);\r
+                }\r
+                else\r
+                {\r
+                    file = path;\r
+                }\r
+            }\r
+            dialog.InitialDirectory = dir;\r
+            dialog.FileName = file;\r
+        }\r
+\r
         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
         {\r
             _main.SetPlayLog(textBoxDebugLog.Text);\r