从文本文件中获取参数

我有一个C#asp.net页面,必须从文本文件中获取用户名/密码信息。

有人可以告诉我怎么样。

文本文件如下所示:(它实际上要大得多,我只有几行)

DATASOURCEFILE=D:\folder\folder var1= etc var2= more var3 = misc var4 = stuff USERID = user1 PASSWORD = pwd1 

我需要的只是该文件中的UserID和密码。

谢谢您的帮助,

史蒂夫

这可行:

 var dic = File.ReadAllLines("test.txt") .Select(l => l.Split(new[] { '=' })) .ToDictionary( s => s[0].Trim(), s => s[1].Trim()); 

dic是一个字典,因此您可以轻松提取您的值,即:

 string myUser = dic["USERID"]; string myPassword = dic["PASSWORD"]; 

打开文件,在换行符上拆分,然后在每个项目的=上再次拆分,然后将其添加到字典中。

 string contents = String.Empty; using (FileStream fs = File.Open("path", FileMode.OpenRead)) using (StreamReader reader = new StreamReader(fs)) { contents = reader.ReadToEnd(); } if (contents.Length > 0) { string[] lines = contents.Split(new char[] { '\n' }); Dictionary mysettings = new Dictionary(); foreach (string line in lines) { string[] keyAndValue = line.Split(new char[] { '=' }); mysettings.Add(keyAndValue[0].Trim(), keyAndValue[1].Trim()); } string test = mysettings["USERID"]; // example of getting userid } 

您可以使用正则表达式来提取每个变量。 您可以一次读取一行,也可以将整个文件读成一个字符串。 如果是后者,您只需在表达式中查找换行符。

此致,莫滕

不需要字典。 老式解析可以做得更多,可执行代码更少,编译数据量相同,处理更少:

 public string MyPath1; public string MyPath2; ... public void ReadConfig(string sConfigFile) { MyPath1 = MyPath2 = ""; // Clear the external values (in case the file does not set every parameter). using (StreamReader sr = new StreamReader(sConfigFile)) // Open the file for reading (and auto-close). { while (!sr.EndOfStream) { string sLine = sr.ReadLine().Trim(); // Read the next line. Trim leading and trailing whitespace. // Treat lines with NO "=" as comments (ignore; no syntax checking). // Treat lines with "=" as the first character as comments too. // Treat lines with "=" as the 2nd character or after as parameter lines. // Side-benefit: Values containing "=" are processed correctly. int i = sLine.IndexOf("="); // Find the first "=" in the line. if (i <= 0) // IF the first "=" in the line is the first character (or not present), continue; // the line is not a parameter line. Ignore it. (Iterate the while.) string sParameter = sLine.Remove(i).TrimEnd(); // All before the "=" is the parameter name. Trim whitespace. string sValue = sLine.Substring(i + 1).TrimStart(); // All after the "=" is the value. Trim whitespace. // Extra characters before a parameter name are usually intended to comment it out. Here, we keep them (with or without whitespace between). That makes an unrecognized parameter name, which is ignored (acts as a comment, as intended). // Extra characters after a value are usually intended as comments. Here, we trim them only if whitespace separates. (Parsing contiguous comments is too complex: need delimiter(s) and then a way to escape delimiters (when needed) within values.) Side-drawback: Values cannot contain " ". i = sValue.IndexOfAny(new char[] {' ', '\t'}); // Find the first " " or tab in the value. if (i > 1) // IF the first " " or tab is the second character or after, sValue = sValue.Remove(i); // All before the " " or tab is the parameter. (Discard the rest.) // IF a desired parameter is specified, collect it: // (Could detect here if any parameter is set more than once.) if (sParameter == "MyPathOne") MyPath1 = sValue; else if (sParameter == "MyPathTwo") MyPath2 = sValue; // (Could detect here if an invalid parameter name is specified.) // (Could exit the loop here if every parameter has been set.) } // end while // (Could detect here if the config file set neither parameter or only one parameter.) } // end using }