使用LINQ读取文本文件并更新到类

时间:2013-10-28 19:33:52

标签: c# .net linq

我有一个静态类:

static class ConfigurationDetails
{
    public static string Server;//
    public static string username;// 
    public static string password;//
    public static string folder;//=
    public static string sqlserver;//
}

以及格式如下的textFile:

Server = Server
username = administrator
password = "*****"
folder = SPFolder
sqlserver=127.8.9.1

我想阅读文本文件并更新类值,我正在这样做:

List<string> c = File.ReadAllLines(@"C:\Config.txt").ToList();
ConfigurationDetails.Server = c[0];
ConfigurationDetails.username = c[1];
ConfigurationDetails.password = c[2];
ConfigurationDetails.folder = c[3];
ConfigurationDetails.sqlserver = c[4];

通过使用LINQ和Lambda,我编写代码的方式可能会有很大改进。那么有什么建议吗?

3 个答案:

答案 0 :(得分:1)

你确定LINQ是你问题的答案吗?这实际上看起来像是T4 Templates的工作。

答案 1 :(得分:0)

如果你真的想在你的代码中想要一些lambda - 请检查一下; - )

    public void Init()
    {
        var lines = File.ReadAllLines(@"C:\Config.txt");
        Array.ForEach(lines, ParseConfigLine);
    }

    private static void ParseConfigLine(string line)
    {
        var separatorIndex = line.IndexOf('=');
        if (separatorIndex < 1)
        {
            // you got malformed line without parameter name - raise an error
            throw new .... 
        }

        Action<string> setParameterAction = null;
        string parameterName = line.Substring(0, separatorIndex).Trim();
        switch (parameterName)
        {
            case "Server":
                setParameterAction = v => ConfigurationDetails.Server = v;
                break;
            case "username":
                setParameterAction = v => ConfigurationDetails.UserName = v;
                break;
            case "password":
                setParameterAction = v => ConfigurationDetails.Password = v;
                break;
            case "folder":
                setParameterAction = v => ConfigurationDetails.Folder = v;
                break;
            case "sqlserver":
                setParameterAction = v => ConfigurationDetails.SqlServer = v;
                break;
            default:
                // unknown parameter - raise an error or ignore;
                return;
        }

        string parameterValue = separatorIndex == line.Length - 1
                                    ? string.Empty
                                    : line.Substring(separatorIndex).Trim();

        setParameterAction(parameterValue);
    }

答案 2 :(得分:0)

如果你想使用一点LINQ,你应该这样做:

static class ConfigurationDetails {
  private static string[] cores = new string[5];
  public static string Server { // = iic-belfort
     get { return cores[0];}
     set { cores[0] = value;}
  }
  public static string username { // = administrator
     get { return cores[1];}
     set { cores[1] = value;}
  }
  public static string password { //= GARBLED
     get { return cores[2];}
     set { cores[2] = value;}
  }
  public static string folder { //= iicservices
     get { return cores[3];}
     set { cores[3] = value;}
  }
  public static string sqlserver { //=127.8.9.1   
     get { return cores[4];}
     set { cores[4] = value;}
  }
  public static void Init(string[] coreData) {
     cores = coreData;
  }
}
//Then do something like this:
var c = File.ReadAllLines(@"C:\Config.txt")
            .Select(line=>line.Split('=')[1].Trim()).ToArray();
ConfigurationDetails.Init(c);