简体   繁体   English

如何将桌面路径添加到app.config文件?

[英]How to add desktop path to app.config file?

I am creating an C# win form application where user can specify any location to save a file. 我正在创建一个C#Win Form应用程序,用户可以在其中指定任何位置来保存文件。 But, in default I need to provide the desktop path. 但是,默认情况下,我需要提供桌面路径。 But the desktop path changes depend on the Windows version, I found that: Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 但是桌面路径的更改取决于Windows版本,我发现: Environment.GetFolderPath(Environment.SpecialFolder.Desktop); can get the desktop path of any windows version. 可以获取任何Windows版本的桌面路径。 But the problem is how do I add this code to App.config file? 但是问题是如何将这段代码添加到App.config文件中? Is there are special xml code block for this. 是否为此有特殊的xml代码块。

You have to add a settings-file to your project. 您必须将设置文件添加到您的项目。 There you need to create a property in the application-scope. 在那里,您需要在application-scope中创建一个属性。 This property you can access by 您可以通过访问此属性

string path = Properties.Settings.Default.Path;
if(string.IsNullOrEmpty(path))
{
   path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}

Go through AppDomain.GetData and AppDomain.SetData 通过AppDomain.GetDataAppDomain.SetData
Have a constant like DESKTOP_DIRECTORY in you application & set its value to SpecialFolders.Desktop 在您的应用程序中有一个类似DESKTOP_DIRECTORY的常量,并将其值设置为SpecialFolders.Desktop
You can then use AppDomain.GetData to retrieve it back. 然后,您可以使用AppDomain.GetData将其取回。
You can then use it in your App.config file. 然后,您可以在App.config文件中使用它。

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

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