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())
Related
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 2 years ago.
Improve this question
I have a List as following
List<string> email = File.ReadAllLines(fileName).ToList<string>();
filename is text I openFileDialog. The text in file txt is 3 line with 3 gmail
I want convert List to
string[] arRcpt = new string[] { };
The File.ReadAllLines(fileName) returns a string[]. You don't need to convert it into a list and then to string[]. You can directly assign it to an array.
string[] arRcpt = File.ReadAllLines(fileName)
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 6 years ago.
Improve this question
I found application that use .txt files as config files.
The Files looks like
[[plugin.save]]=[[Save]]
how do I use it in C#?
how can I read The file to my application to use Values in [] as config?
You have to read it as a regular file. Reading it use Dictionary to store values.
Example code:
Dictionary<string, string> configuration = new Dictionary<string, string>();
Regex r = new Regex(#"\[\[(\w+)\]\]=\[\[(\w+)\]\]");
string[] configArray = {"[[param1]]=[[Value1]]", "[[param2]]=[[Value2]]"};// File.ReadAllLines("some.txt");
foreach (string config in configArray)
{
Match m = r.Match(config);
configuration.Add(m.Groups[1].Value, m.Groups[2].Value);
}
Please note to check for possible null values.
Note also that regex expression should be different if config values can contain for example spaces.
Just use:
var config = File.ReadAllLines(FileLocation)
And then parse it with the
String.Split()
Might use regex as well.
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 6 years ago.
Improve this question
This is a sample of the textdelimited file
"11- 4-2014","20:54:22","","3974","1","1","1"
"11- 4-2014","20:55:25","","1411","1","1","1"
"11- 4-2014","20:55:26","","3177","1","1","1"
"11- 4-2014","20:55:32","","4051","1","1","1"
I need it to parse and write to a text file looking like
ID DateTime Area Numb1 Num2 Num3
1 11/4/2014-20:47:48 4297 1 1 1
2 11/4/2014-20:52:03 4013 1 1 1
The validation part comes in to check if 'Area' actually has 4 numbers, if it doesn't, Write all errors to a separate text file in the same format.
Check out this answer:
https://stackoverflow.com/a/20523165/4875896
quote:
Add a reference to Microsoft.VisualBasic.dll (works fine in C#, don't mind the name)
using (TextFieldParser parser = new TextFieldParser(#"c:\temp\test.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
//Process row
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
//TODO: Validate field and save as needed.
}
}
}
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 am trying read one string from TextBox and use in my script.
Suppose this TextBox name is: txt3.
I want read this value and use in below lines:
string s = Regex.Replace(str,
#"\btxt3.Text\b",
txt4.Text,
RegexOptions.IgnoreCase);
How I can write this #"\btxt3.Text\b" ?
I want write that as :
string str==#"\btxt3.Text\b";
You want something like:
String.Format(#"\b{0}\b", txt3.Text)
Try this
string s = Regex.Replace(str, string.Format(#"\b{0}\b",txt3.Text), txt4.Text, RegexOptions.IgnoreCase);
If you want to combine value of txt3 with other strings, one way to do it is write
"\\b" + txt3.Text + "\\b"
instead of
#"\btxt3.Text\b"
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.