C#读取文件并拉出特定行
提前谢谢。我目前拥有的是一个选择文本文件的文件对话框。文本文件内容看起来像example.txt,它需要看起来像output.txt。注意:字符串C#读取文件并拉出特定行,c#,C#,提前谢谢。我目前拥有的是一个选择文本文件的文件对话框。文本文件内容看起来像example.txt,它需要看起来像output.txt。注意:字符串CoreDBConnectString=是一行,一直到;数据库=源数据库。 example.txt [Information] Date= CreatedBy= Unique=eqwe-asd-123-as12-3 CoreDataSource= CoreDBConnectString=Provider=SQLOLEDB.1;Server=Server
CoreDBConnectString=
是一行,一直到;数据库=源数据库。
example.txt
[Information]
Date=
CreatedBy=
Unique=eqwe-asd-123-as12-3
CoreDataSource=
CoreDBConnectString=Provider=SQLOLEDB.1;Server=Server;Integrated Security=SSPI;Database=Source_DB
NoteDataSource=SQLServer
NoteDBConnectString=Provider=Provider=SQLOLEDB.1;Server=Server;Integrated Security=SSPI;Database=Source_DB
CoreDBCaseID=99
NoteDBCaseID=99
Output.txt
Table=99 (Comes from CoreDBCaseID)
Server=Server (comes from the string CoreDBConnectString=)
Security=SSPI (comes from the string CoreDBConnectString=)
Database=Source_DB (comes from the string CoreDBConnectString=)
example.txt
[资料]
日期=
创造的=
唯一=eqwe-asd-123-as12-3
核心数据源=
CoreDBConnectString=Provider=SQLOLEDB.1;服务器=服务器;综合安全=SSPI;数据库=源数据库
NoteDataSource=SQLServer
NoteDBConnectString=Provider=Provider=SQLOLEDB.1;服务器=服务器;综合安全=SSPI;数据库=源数据库
CoreDBCaseID=99
NoteDBCaseID=99
Output.txt
表=99(来自CoreDBCaseID)
Server=Server(来自字符串CoreDBConnectString=)
Security=SSPI(来自字符串CoreDBConnectString=)
Database=Source\u DB(来自字符串CoreDBConnectString=)我将使用和的组合来读取每一行并提取适当的信息。我将使用和的组合来读取每一行并提取适当的信息。这是一个INI文件,非常古老。现在,我们编程人员使用XML代替。因此,C#中没有对INI文件的内置支持 您可以选择读取数据 如果您不介意信息部分标题,可以使用将新数据写出,如下所示:
[Information]
Table=99
Server=Server
Security=SSPI
Database=Source_DB
可能会有帮助。这是一个INI文件,非常陈旧。现在,我们编程人员使用XML代替。因此,C#中没有对INI文件的内置支持 您可以选择读取数据 如果您不介意信息部分标题,可以使用将新数据写出,如下所示:
[Information]
Table=99
Server=Server
Security=SSPI
Database=Source_DB
可能会有帮助。打开文件,一次一行读取。您可以使用正则表达式只匹配和解析要查找的文本。打开文件,一次一行读取。您可以使用正则表达式仅匹配和解析要查找的文本。INI文件是旧的skool。因此,有人已经为它编写了一个类:。链接的类读取XML、INI、注册表和配置文件。INI文件是旧的skool。因此,有人已经为它编写了一个类:。链接的类读取XML、INI、注册表和配置文件。您可以执行以下操作:
// Load the file contents
string contents = File.ReadAllText("example.txt");
// Obtain the data using regular expressions
string id = string id = Regex.Match(
contents,
@"CoreDBCaseID=(?<id>\d+)").Groups["id"].Value;
string server = string.Empty; // Add regex here
string security = string.Empty; // Add regex here
string database = string.Empty; // Add regex here
// Save the data in the new format
string[] data = new string[] {
String.Format("Table={0}", id),
String.Format("Server={0}", server),
String.Format("Security={0}", security),
String.Format("Database={0}", database)
};
File.WriteAllLines("output.txt", data);
//加载文件内容
字符串内容=File.ReadAllText(“example.txt”);
//使用正则表达式获取数据
string id=string id=Regex.Match(
目录
@“CoreDBCaseID=(?\d+))。组[“id”]。值;
string server=string.Empty;//在此处添加正则表达式
字符串安全性=string.Empty;//在此处添加正则表达式
字符串数据库=string.Empty;//在此处添加正则表达式
//以新格式保存数据
字符串[]数据=新字符串[]{
格式(“Table={0}”,id),
格式(“Server={0}”,Server),
格式(“Security={0}”,Security),
格式(“数据库={0}”,数据库)
};
writeAllines文件(“output.txt”,数据);
还有一种快速学习正则表达式的方法:
您可以使用停止(;)字符处的匹配。大概是这样的:
@“安全=(?[\D]+)(?=;)”您可以这样做:
// Load the file contents
string contents = File.ReadAllText("example.txt");
// Obtain the data using regular expressions
string id = string id = Regex.Match(
contents,
@"CoreDBCaseID=(?<id>\d+)").Groups["id"].Value;
string server = string.Empty; // Add regex here
string security = string.Empty; // Add regex here
string database = string.Empty; // Add regex here
// Save the data in the new format
string[] data = new string[] {
String.Format("Table={0}", id),
String.Format("Server={0}", server),
String.Format("Security={0}", security),
String.Format("Database={0}", database)
};
File.WriteAllLines("output.txt", data);
//加载文件内容
字符串内容=File.ReadAllText(“example.txt”);
//使用正则表达式获取数据
string id=string id=Regex.Match(
目录
@“CoreDBCaseID=(?\d+))。组[“id”]。值;
string server=string.Empty;//在此处添加正则表达式
字符串安全性=string.Empty;//在此处添加正则表达式
字符串数据库=string.Empty;//在此处添加正则表达式
//以新格式保存数据
字符串[]数据=新字符串[]{
格式(“Table={0}”,id),
格式(“Server={0}”,Server),
格式(“Security={0}”,Security),
格式(“数据库={0}”,数据库)
};
writeAllines文件(“output.txt”,数据);
还有一种快速学习正则表达式的方法:
您可以使用停止(;)字符处的匹配。大概是这样的:
@“Security=(?[\D]+)(?=;)”我们也使用屈尊俯就,更喜欢省略标点符号我们也使用屈尊俯就,更喜欢省略标点符号这很好,我在Regex很新,我做了以下工作。字符串security=Regex.Match(内容@“security=(?\D+))。组[“security”]。值;这使得:安全性=SSPI;Database=GSK_DRP_Paxil_SSECase我如何在;所以我会得到Security=SSPI,但效果很好!这很好,我在Regex很新,我做了以下工作。字符串security=Regex.Match(内容@“security=(?\D+))。组[“security”]。值;这使得:安全性=SSPI;Database=GSK_DRP_Paxil_SSECase我如何在;所以我会得到Security=SSPI,但效果很好!