HI 高手们:我现在有个文档内容是:
COM=COM3
Baudrate=19200
Parity=Even
DataBits=7
StopBits=One目的:
我要读取这里的每一行,获取每一行等于号右边的内容放入一个string数组现在代码:private string text()
{
string[] comAry = new string[4]{};
string Path = "text.txt";
FileStream fs = new FileStream(Path , Path.Open);
StreamReader sr = new StreamReader(fs);
line = sr.ReadLine();
while (line.IndexOf("=") > -1)
{
comAry[i] = line.Substring(line.IndexOf("="), line.Length - line.IndexOf("="));
i++;
line = sr.ReadLine();
}
}问题:
我现在获取到的内容会把等于号一起加进去,请问有什么办法解决
COM=COM3
Baudrate=19200
Parity=Even
DataBits=7
StopBits=One目的:
我要读取这里的每一行,获取每一行等于号右边的内容放入一个string数组现在代码:private string text()
{
string[] comAry = new string[4]{};
string Path = "text.txt";
FileStream fs = new FileStream(Path , Path.Open);
StreamReader sr = new StreamReader(fs);
line = sr.ReadLine();
while (line.IndexOf("=") > -1)
{
comAry[i] = line.Substring(line.IndexOf("="), line.Length - line.IndexOf("="));
i++;
line = sr.ReadLine();
}
}问题:
我现在获取到的内容会把等于号一起加进去,请问有什么办法解决
using(StreamReader reader=new StreamReader(@"E:\Test.txt"))
{
string line=string.Empty;
List<string> result=new List<string>();
while(line!=null)
{
line=reader.ReadLine();
if(string.IsNullOrEmpty(line))
{
continue;
} if(!line.Contains("="))
{
continue;
} result.Add(line.Spilt('=')[1]);
}
}
string[] comAry = null; comAry = File.ReadAllLines(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312")).Where(a => a.IndexOf("=") > 0).Select(a => { return Regex.Match(a, @"(?<==\s*)[^\s]+").Value; }).ToArray();
/*
[0] "COM3" string
[1] "19200" string
[2] "Even" string
[3] "7" string
[4] "One" string */
{
List<string> comAry = new List<string>();
int i = 0;
string Path = "text.txt";
FileStream fs = new FileStream(Path,FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
while (line!= null && line.IndexOf("=") > -1)
{
comAry.Add(line.Substring(line.IndexOf("=") + 1, line.Length - line.IndexOf("=") - 1));
i++;
line = sr.ReadLine();
}
}
string a = "company=Hello world";
int i = a.IndexOf('=');
if(i >= 0)
{
string v = a.Substring(i + 1);
MessageBox.Show(v);
}
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
);
还有其他一系列以GetPrivateProfile开头的函数,查一下就有了。