创建应用程序配置文件,如果不存在C# [英] Create app config file if doesn't exist C#

查看:414
本文介绍了创建应用程序配置文件,如果不存在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图通过

ConfigurationManager.OpenExeConfiguration(filePath);

它返回一个例外,因为没有文件。但我需要建立在这种情况下,这个文件。添加应用程序:可是我所知,配置文件中加入在VS如如何创建配置文件到C#项目

那么,如何在其他的方式这个文件?

So, how to create this file in other way?

推荐答案

如果您的真正的要创建一个空的配置文件在运行时你可以做这样的事情,但需要注意的是,如果配置文件已经存在,这个code将覆盖它

If you really want to create an empty config file at runtime you can do something like this, but note that if the config file already exists this code will overwrite it:

System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sb.AppendLine("<configuration>");
sb.AppendLine("</configuration>");

string loc = Assembly.GetEntryAssembly().Location;
System.IO.File.WriteAllText(String.Concat(loc, ".config"), sb.toString());

这篇关于创建应用程序配置文件,如果不存在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆