user222427
user222427

Reputation:

C# Reading file and pulling out specific lines

Thanks in advance. What i currently have is a filedialog box selecting a text file. The text file contents will look like example.txt, and it needs to look like output.txt. NOTE: the string CoreDBConnectString= is 1 line all the way to ;Database=Source_DB.

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=)

Upvotes: 0

Views: 604

Answers (5)

João Angelo
João Angelo

Reputation: 57658

You can do something like this:

// 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);

And a quick way to learn those regular expressions:

Regular Expression Cheat Sheet

Regular-Expressions.info


You can use a positive lookahead to stop matching at the (;) character. Something like this:

@"Security=(?[\D]+)(?=;)"

Upvotes: 3

Mongus Pong
Mongus Pong

Reputation: 11477

That is an INI file, which is very old school. Nowadays, we programming folks use XML instead. Consequently, there is no built-in support for INI files in C#.

You can P/Invoke GetPrivateProfileString to read the data.

You can use WritePrivateProfileString to write the new data out if you don't mind the information section header like so :

[Information]
Table=99
Server=Server 
Security=SSPI 
Database=Source_DB

This CodeProject article on INI-file handling with C# may help.

Upvotes: 2

user195488
user195488

Reputation:

INI files are old skool. Thus, someone has already written a class for it: http://www.codeproject.com/KB/cs/readwritexmlini.aspx. The class linked reads XML, INI, Registry, and Config Files.

Upvotes: 0

TLiebe
TLiebe

Reputation: 7966

Open the file and read it in one line at a time. You can use Regular Expressions (cheat sheet) to match and parse only the text you want to find.

Upvotes: 0

Jeff Yates
Jeff Yates

Reputation: 62367

I would use a combination of StreamReader.ReadLine and RegEx to read each line and extract the appropriate information.

Upvotes: 0

Related Questions