Reading a text file and getting information by line C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
When reading a text file by using StreamReader, how can I get information from the contents on that line
for example:
Line 1: Hi there, my name is bob
Line 2: 1234563 90312
Line 3: I jumped over the moon
How would I detect 90312 on line 2? or anything else I may want on other lines?
Thanks.

You can check whether the current line contains some specific string. Such as:
string line;
using (StreamReader reader = new StreamReader("file.txt"))
{
line = reader.ReadLine();
if(line.Contains("90312"))
{
// Do something fancy here.
}
}

As far as I understand your question, you probably want to read a line into a variable and then tokenize it based on word-to-word basis.
You can use > stringObject.Split(' '); where, stringObject contains the line in question.

Related

Calling a txt list from within another txt list c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have six txt files. And one of them holding the others names in inside. For example i have a txt file called brands. And inside of brands there are names of cars brands for example: bmv, toyota, audi...
And also there are files who exactly same names (bmv.txt, toyota.txt, audi.txt).
enter image description here
(Sorry for my english its not my native language)
My questions is can i create arrays for each car using for loop?
Although this question is rather vague, I will give you a basic flow for what you need to do.
Read File names for each category and store them.
Then Read the Contents of each category and match the values with the filenames.
Read Contents of each sub category file.
You are going to use the following Code Samples to Achieve this.
Get Filenames in Directory:
string [] fileEntries = Directory.GetFiles(targetDirectory);
Read Contents of File:
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
This is a basic approach that should get you started.
Good Luck.

How to export CSV to Db with headers in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to export csv to db in C#. It works fine when you have a plain text simple text. But in my case I need to parse headers as single row.
There are many options!
use some driver custom functions (depends on your database)
use a [schema.ini](
https://learn.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver)
do it manually ...
You can do it manually with while iteration. Before using loop, you should read csv file from path with StreamReader.
For example:
using(var csvReader = new StreamReader(System.IO.File.OpenRead("your file path")))
{
while (!csvReader.EndOfStream)
{
var rows = csvReader.ReadLine();
var columns = rows.Split(';');
if(values.Length > 1)
{
// Your logic getting values to save db
}
}
}

How to display HashSet<SortedSet<string>> collection in richTextBox? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have HashSet> collection. I want to display content of this HashSet in richTextBox ? How to convert this HashSet to right type ?
Try :
richTextBox.Text = string.Join("", yourSet.ToArray());
try this instead:
foreach (SortedSet<string> set in youHashSet)
{
richTextBox.Text = string.Join("", set.ToArray());
}
Well a little bit of more information would have helped. It's hard to say without seeing your code. But these are your basic steps
after you build your Hashset
then for a for or foreach loop on it
and then ToString the object if it's not a Hashset
string helloWorld = string.empty;
foreach(<whateverobject> myobject in Hashset<whateverobject> myHashsetList)
{
helloWorld = helloWorld = myobject.ToString();
}
richTextBox.Text = helloWorld;
If you delimit it or whatever that's up to you

C# How to modify text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to read accounts.txt and add/change number after password
Here is accounts.txt
user|password
user1|password1
After starting
user|password|1
user1|password1|1
After closing
user|password|0
user1|password1|0
Sorry for my english
Add this reference first System.IO Then:
For reading:
string[] accounts= File.ReadAllLines("accounts.txt");
//each item of array will be your account
For modifying :
accounts[0] += "|1";//this adds "|1" near password
accounts[0].Replace("|1","|0"); this will change the "|0" text to "|1"
And For writing:
File.WriteAllLines("accounts.txt",accounts);
Look at System.IO.File class, as well as general System.IO.Stream and System.IO.StreamReader classes.
There are a bunch of examples on the internet on how to read and write text files in .NET: http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx
Something like that :
string[] lines = File.ReadAllLines(#"C:\filepath.txt");
List<string> newlines = new List<string>();
foreach(string line in lines)
{
string[] temp = line.Split('|');
newlines.Add(temp[0] + "|" + temp[1] + "|" + "1");
}
File.WriteAllLines(#"C:\filepath.txt", newlines.ToArray())

How to connect my c# programme to a text file and edit parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I created an application which has 2 labels and 1 textbox.
The first label will show the total number of days since the year began, while the second shows the number of tests in the year. In the textbox shows the number of tests that I have not studied.
When the program opens, you have to click a button to load an .txt file to get these numbers, they are saved when you exit by clicking in another button. I Wanted to know how to load the file and to save the parameters.
I'm developing in C#, grateful, sorry for bad english.
To read text use File.ReadAllLines
List<string> lines = File.ReadAllLines(path).ToList();
To write text use File.WriteAllLines
File.WriteAllLines(path, lines.ToArray());
Or some variation on those methods to suit your needs.
One approach would be this:
using System.IO;
using (var reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// Do stuff with your line here, it will be called for each
// line of text in your file.
}
}
Or, you could do:
List<String> txtStrings = File.ReadAllLines(path).ToList();
Or, come up with your own approach by reading up on this post.

Categories