Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在其他设置上构建C设置值_C#_.net_App Config - Fatal编程技术网

C# 在其他设置上构建C设置值

C# 在其他设置上构建C设置值,c#,.net,app-config,C#,.net,App Config,我来自Java世界-Java属性文件可以通过以下方式基于彼此构建 base.dir=C:\dev\basedir logging.dir=$(base.dir)\logs # results in C:\dev\basedir\logs output.dir=$(base.dir)\output # results in C:\dev\basedir\output 如何使用设置和app.config文件在.NET项目中实现同样的功能?我花了很长时间搜索,但没有找到任何有效的 app.conf

我来自Java世界-Java属性文件可以通过以下方式基于彼此构建

base.dir=C:\dev\basedir
logging.dir=$(base.dir)\logs  # results in C:\dev\basedir\logs
output.dir=$(base.dir)\output # results in C:\dev\basedir\output
如何使用设置和app.config文件在.NET项目中实现同样的功能?我花了很长时间搜索,但没有找到任何有效的

app.config中的以下方法不起作用,返回文本值

<setting name="baseDir" serializeAs="String">
   <value>C:\dev\basedir</value>
</setting>
<setting name="logDir" serializeAs="String">
   <value>$(baseDir)log\</value>
</setting>

我不熟悉从app.config文件中执行此操作的方法。不过,在程序中编写这样的功能是相当容易的

<setting name="baseDir" serializeAs="String">
   <value>C:\dev\basedir</value>
</setting>
<setting name="logDir" serializeAs="String">
   <value>log\</value>
</setting>

为我自己的问题提供后续答案

假设属性文件如下所示:

## Directories
base.dir=C:\PdfProcessing
archive.dir=${base.dir}\archive
manual.process.dir=${base.dir}\manual_process
下面的类将属性文件解析为映射

static class PropertiesFileReader
{
    private static string PathToPropertiesFile = @"application.properties";
    private static Regex subRegex = new Regex(@"\$\{.*\}");

    /// <summary>
    /// Parses the properties file into a map 
    /// </summary>
    /// <returns></returns>
    public static Dictionary<string, string> ReadProperties() {
        var data = new Dictionary<string, string>();

        foreach (var row in File.ReadAllLines(PathToPropertiesFile))
        {
            var newRow = String.Empty;

            // ignore any rows that are commented out
            // or do not contain an equals sign
            if (!row.StartsWith("#") && row.Contains("="))
            {
                // replace each instance of a variable placeholder in the property value
                foreach (Match match in subRegex.Matches(row))
                {
                    var key = Regex.Replace(match.ToString(), @"[${}]", String.Empty);
                    var replaceValue = data[key];
                    newRow = row.Replace(match.ToString(), replaceValue);
                }

                // add the property key and value
                if (!String.IsNullOrEmpty(newRow))
                {
                    data.Add(newRow.Split('=')[0], string.Join("=", newRow.Split('=').Skip(1).ToArray()));
                }
                else
                {
                    data.Add(row.Split('=')[0], string.Join("=", row.Split('=').Skip(1).ToArray()));
                }
            }


        }


        return data;

    }

}

您在Java属性文件中显示的内容不是Java.util.properties的固有部分。。。也许你在它们上面使用了什么?@JonSkeet-是的,在java.util上面使用了xproperty。Properties@ThomasLindvall-提供的链接如何与我所问的关于相互建立价值的问题相关?好的,所以,当你说Java属性文件时,你指的是带有第三方库的Java属性——我猜想,你可以很容易地为app.config自己构建一个类似的库
static class PropertiesFileReader
{
    private static string PathToPropertiesFile = @"application.properties";
    private static Regex subRegex = new Regex(@"\$\{.*\}");

    /// <summary>
    /// Parses the properties file into a map 
    /// </summary>
    /// <returns></returns>
    public static Dictionary<string, string> ReadProperties() {
        var data = new Dictionary<string, string>();

        foreach (var row in File.ReadAllLines(PathToPropertiesFile))
        {
            var newRow = String.Empty;

            // ignore any rows that are commented out
            // or do not contain an equals sign
            if (!row.StartsWith("#") && row.Contains("="))
            {
                // replace each instance of a variable placeholder in the property value
                foreach (Match match in subRegex.Matches(row))
                {
                    var key = Regex.Replace(match.ToString(), @"[${}]", String.Empty);
                    var replaceValue = data[key];
                    newRow = row.Replace(match.ToString(), replaceValue);
                }

                // add the property key and value
                if (!String.IsNullOrEmpty(newRow))
                {
                    data.Add(newRow.Split('=')[0], string.Join("=", newRow.Split('=').Skip(1).ToArray()));
                }
                else
                {
                    data.Add(row.Split('=')[0], string.Join("=", row.Split('=').Skip(1).ToArray()));
                }
            }


        }


        return data;

    }

}
private static readonly Dictionary<string, string> props = PropertiesFileReader.ReadProperties();
props["archive.dir"] # C:\PdfProcessing\archive