简体   繁体   English

保存文件对话框 - 路径无效

[英]Save file dialog - path not working

App.config: App.config中:

<add key="SaveDraftPath" value="C:\Drafts\"/>

C#: C#:

var saveDraftPath = ConfigurationManager.AppSettings["SaveDraftPath"]; 
var sfDialog = new SaveFileDialog();
sfDialog.InitialDirectory = saveDraftPath;
sfDialog.FileName = "FILE";

For some reason this doesn't open the filebrowser in the path like planned, any one know why or how to fix? 出于某种原因,这不会在计划的路径中打开文件浏览器,任何人都知道为什么或如何修复?

I've tried this now, still doesn't work: 我现在试过这个,仍然无法正常工作:

var saveDraftPath = Path.GetFullPath(ConfigurationManager.AppSettings["SaveDraftPath"]);
MessageBox.Show("does directory exist : " + Directory.Exists(saveDraftPath));
var sfDialog = new SaveFileDialog();
sfDialog.InitialDirectory = saveDraftPath;
sfDialog.FileName = "FILE";

and Directory.Exists(saveDraftPath) returns true.. Hmmm?! 和Directory.Exists(saveDraftPath)返回true ..嗯?!

Edit: The above code has worked once for me. 编辑:上面的代码对我有用。 The code works for everyone who has so far answered. 该代码适用于迄今为止已回答的所有人。 But it is still not working. 但它仍然无法正常工作。 So I suspect the problem is some sort of local/history setting stopping it. 所以我怀疑问题是某种本地/历史设置阻止了它。 Does anyone know why this might happen? 有谁知道为什么会这样?

Try this: 尝试这个:

var path = Path.GetFullPath(ConfigurationManager.AppSettings["SaveDraftPath"]) var path = Path.GetFullPath(ConfigurationManager.AppSettings [“SaveDraftPath”])

Have a look at Path Class as well, has got several helpful methods 看看Path Class,也有几个有用的方法

this worked for me (getting the correct path from the config) 这对我有用(从配置中获取正确的路径)

var saveDraftPath =
           ConfigurationManager.AppSettings["SaveDraftPath"];
        var sfDialog = new SaveFileDialog();
        sfDialog.InitialDirectory = saveDraftPath;
        sfDialog.FileName = "FILE";

        if (sfDialog.ShowDialog() == DialogResult.OK)
        {
            //do stuff
        }

see for more http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx 了解更多信息http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM