{"id":359,"date":"2011-12-08T17:40:29","date_gmt":"2011-12-08T09:40:29","guid":{"rendered":"http:\/\/www.whitetrefoil.com\/blog\/?p=359"},"modified":"2011-12-08T17:40:29","modified_gmt":"2011-12-08T09:40:29","slug":"wpf%e4%bd%bf%e7%94%a8system-windows-openfiledialog%e9%80%89%e6%8b%a9%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/www.whitetrefoil.com\/blog\/?p=359","title":{"rendered":"WPF\u4f7f\u7528System.Windows.OpenFileDialog\u9009\u62e9\u6587\u4ef6\u5939"},"content":{"rendered":"<p>\u8fd9\u51e0\u5929\u5728\u91cd\u6e29C#.Net-WPF\uff0c\u62ff\u4e2a\u4ee5\u524d\u5e9f\u5f03\u7684\u9879\u76ee\u7ec3\u7ec3\u624b\uff0c\u5176\u4e2d\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>WPF\u81ea\u8eab\u7684\u547d\u540d\u7a7a\u95f4System.Windows\u4e2d\u6ca1\u6709\u6253\u5f00\u6587\u4ef6\u5939\u7684\u5bf9\u8bdd\u6846\uff0c\u53ea\u6709OpenFileDialog\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<p>\u7f51\u4e0a\u641c\u4e86\u5f88\u591a\u5e16\u5b50\uff0c\u65e0\u5916\u4e4e\u4e09\u79cd\u7b54\u6848<\/p>\n<ol>\n<li>\u4f7f\u7528\u4ee5\u524dWinForm\u7684FolderBrowserDialog\uff1a\u4e3a\u4e86\u907f\u514d\u547d\u540d\u7a7a\u95f4\u51b2\u7a81\u5bfc\u81f4\u7684\u4ee3\u7801\u7e41\u7410\uff08\u4e24\u8fb9\u90fd\u6709OpenFileDialog\uff09\u4ee5\u53ca\u51fa\u4e8e\u4e00\u4e9b\u6d01\u7656\u7684\u539f\u56e0\uff0c\u6211\u8fd9\u6b21\u51b3\u5b9a\u575a\u51b3\u4e0d\u4f7f\u7528WinForm\uff0c\u53cd\u6b63\u81ea\u5df1\u505a\u9879\u76ee\u4e5f\u6ca1\u5de5\u671f\u4ec0\u4e48\u7684\uff0c\u5927\u4e0d\u4e86\u4e0d\u505a\u4e86\uff5e<\/li>\n<li>\u6269\u5199System.Windows.OpenFileDialog\uff1a\u8981\u4e48\u81ea\u5df1\u6269\u5199\uff0c\u8981\u4e48\u7528\u4e00\u4e9b\u522b\u4eba\u5199\u597d\u7684\u5f00\u6e90\u7ec4\u4ef6\uff0c\u4e00\u4e2a\u662f\u592a\u9ebb\u70e6\uff0c\u53e6\u4e00\u4e2a\u5fc3\u91cc\u603b\u89c9\u5f97\u4e0d\u592a\u8212\u670d<\/li>\n<li>\u4f7f\u7528\u5fae\u8f6f\u5b98\u65b9\u7684\u6269\u5c55\u5305Microsoft.WindowsAPICodePack\uff1a\u8fd9\u4e2a\u770b\u8d77\u6765\u5f88\u4e0d\u9519\uff0c\u4f46\u662f\u4e5f\u6709\u7f3a\u70b9\uff0c\u5c31\u662f\u5b8c\u5168\u8981\u6c42Windows\u7248\u672c>=6.0\uff0c\u800c\u516c\u53f8\u7684\u7535\u8111\u8fd8\u662f\u676f\u5177\u76845.1\uff0c\u53ea\u80fd\u653e\u5f03<\/li>\n<\/ol>\n<p>\u6b63\u5728\u7ea0\u7ed3\u4e4b\u9645\uff0c\u56fd\u5916\u8457\u540d\u793e\u533a Stack Overflow \u4e0a\u770b\u5230\u8fd9\u4e48\u4e00\u7bc7\u5e16\u5b50\uff1a<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/31059\/how-do-you-configure-an-openfiledialog-to-select-folders\" title=\".net - How do you configure an OpenFileDIalog to select folders? - Stack Overflow\" target=\"_blank\">\u539f\u5e16\u5730\u5740<\/a><\/p>\n<p>\u4e00\u4f4d\u5927\u4f6c\u60f3\u51fa\u4e86\u4e00\u4e2a\u770b\u8d77\u6765\u5f88orz\u7684\u65b9\u6cd5\uff0c\u4e0d\u8fc7\u53cd\u6b63\u4e5f\u6ca1\u5176\u4ed6\u89e3\u51b3\u65b9\u6848\uff0c\u59d1\u4e14\u4e00\u8bd5\u3002<\/p>\n<p>\u8fd9\u4f4d\u5927\u4f6c\u5c06Filter\u8bbe\u7f6e\u6210\u4e00\u4e2a\u975e\u5e38\u975e\u5e38\u957f\u7684\u6269\u5c55\u540d\u4ece\u800c\u8d77\u5230\u9690\u85cf\u6240\u6709\u6587\u4ef6\u7684\u76ee\u7684\uff1a<\/p>\n<p><code>openFileDialog1.Filter = \"folders|*.neverseenthisfile\"<\/code><\/p>\n<p>\u7136\u540e\u628a\u6587\u4ef6\u540d\u8bbe\u4e3a\u4e00\u4e2a&#8221;r&#8221;\u6362\u884c\u7b26\u6765\u9a97\u8fc7OpenFileDialog\uff0c\u8fd9\u6837\u53ea\u8981\u4e0d\u6539\u52a8\u6587\u4ef6\u540d\u5c31\u80fd\u76f4\u63a5\u6309\u6253\u5f00\u6309\u94ae\u3002\u5f53\u7136\u540c\u65f6\u8fd8\u9700\u8981\u628aCheckFileExists\u7ed9false\u6389\u2026\u2026<\/p>\n<p>\u6211\u4fee\u6539\u4e86\u4ed6\u7684\u539f\u578b\u4ee3\u7801\u540e\u5f97\u5230\u4e86\u5982\u4e0b\u7248\u672c\uff1a<\/p>\n<p><code><\/p>\n<pre>OpenFileDialog ofd = new OpenFileDialog();\nofd.Filter = \"folders|*.sado;fjsdal;fj\";\nofd.FileName = \"r\";\nofd.CheckFileExists = false;\nofd.CheckPathExists = true;\nofd.ValidateNames = false;\nif (ofd.ShowDialog() == true)\n{\n\tofd.FileName = ofd.FileName.TrimEnd('r');\n\tint lastSeparatorIndex = ofd.FileName.LastIndexOf('\\');\n\tofd.FileName = ofd.FileName.Remove(lastSeparatorIndex);\n\ttxtCOutputPath.Text = ofd.FileName;\n}<\/pre>\n<p><\/code><\/p>\n<p>\u8fd0\u884c\u540e\u6210\u529f\u53d6\u5230\u4e86\u76ee\u5f55\uff0c\u5e76\u4e14\u54ea\u6015\u4e0d\u5f53\u5fc3\u5728\u6587\u4ef6\u6587\u672c\u6846\u91cc\u8f93\u5165\u4e86\u5185\u5bb9\u4e5f\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002\u672c\u4ee5\u4e3a\u4e8b\u60c5\u5230\u6b64\u4e3a\u6b62\uff0c\u6211\u751a\u81f3\u90fd\u628a\u8fd9\u7bc7\u535a\u6587\u53d1\u8868\u4e86\u51fa\u6765\uff08\u7b2c\u4e00\u7a3f\u662f\u5230\u8fd9\u884c\u7684\u524d\u4e00\u4e2a\u53e5\u53f7\u5c31\u7ed3\u675f\u7684\uff09\uff0c\u5230\u5bb6\u540e\u518d\u8bd5\u624d\u53d1\u73b0\u5bb6\u91cc\u7684\u5a1c\u5a1c\u4e0d\u4e70\u8d26\uff01\uff01\uff01<\/p>\n<p>\u4ed4\u7ec6\u7814\u7a76\u540e\uff0c\u53d1\u73b0\u8c8c\u4f3c\u5a1c\u5a1c\u4e0d\u8ba4ValidateNames\u5c5e\u6027\uff0c\u4e0d\u8bba\u8fd9\u4e2a\u503c\u662ftrue\u8fd8\u662ffalse\uff0c\u5979\u4e00\u5b9a\u4f1a\u68c0\u67e5\u6587\u4ef6\u540d\u662f\u5426\u5408\u6cd5\u2026\u2026\u597d\u5728\u5979\u5e76\u4e0d\u5728\u610f\u6587\u4ef6\u662f\u5426\u771f\u7684\u5b58\u5728\uff0c\u6240\u4ee5\u4ee3\u7801\u7a0d\u52a0\u4fee\u6539\u8fd8\u662f\u53ef\u4ee5\u7528\uff1a<\/p>\n<p><code><\/p>\n<pre>OpenFileDialog ofd = new OpenFileDialog();\nofd.Filter = CoffeeScript._ofdOutputFilter;\nofd.FileName = \"Filename will be ignored\";\nofd.CheckFileExists = false;\nofd.CheckPathExists = true;\n\/\/ofd.ValidateNames = false;\nif (ofd.ShowDialog() == true)\n{\n\t\/\/ofd.FileName = ofd.FileName.TrimEnd('r');\n\tint lastSeparatorIndex = ofd.FileName.LastIndexOf('\\');\n\tofd.FileName = ofd.FileName.Remove(lastSeparatorIndex);\n\ttxtCOutputPath.Text = ofd.FileName;\n\treturn true;\n}\nreturn false;<\/pre>\n<p><\/code><\/p>\n<p>\u4e0d\u8fc7\u8fd9\u79cd\u505a\u6cd5\u8fd8\u662f\u6709\u4e00\u5b9a\u95ee\u9898\u7684\uff0c\u5982\u679c\u7528\u6237\u4e0d\u5f53\u5fc3\u9009\u4e2d\u4e86\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u90a3\u4ed6\u540e\u9762\u7684\u9ebb\u70e6\u5c31\u5927\u4e86\u2026\u2026\u770b\u6765\u8fd9\u4e2a\u95ee\u9898\u8fd8\u9700\u8981\u518d\u7814\u7a76\u4e00\u4e0b\u2026\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba9System.Windows.OpenFileDialog\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u5939<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[18,22,23,33,42,46,47,48,51,62,67,70],"_links":{"self":[{"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/359"}],"collection":[{"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=359"}],"version-history":[{"count":0,"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/359\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whitetrefoil.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}