I want to edit some text files automatically, but I don't know what to used to do it.
I want to open a file, check all lines, if one line begins with a 'C', I remove first 39-characters, etc .. then save all the file with an other name.
I already have a portion of code :
var car = ligne[0];
if(car != 'C') { continue; }
ligne.Remove(0, 39);
I use StreamReader to read, but what is the simple way to read and save in another file ?
Try File.ReadAllLines, which opens a file, reads everything, closes the file and returns an array containing all lines.
Do your processing....
Then File.WriteAllLines to open a file, write data, and close the file.
Related
How would I go about saving a user form input from a view to an existing text file with the ASP.NET MVC framework?
So to start of, you would accept some code from the user and then save it:
string userInput =Console.Readline();
Then you would use the code WriteAllText(). This allows you to create a new file and writes the contents to it. If the file already exists, it will be overwritten.
So basically:
string userInput =Console.Readline();
File.WriteAllText("thenameyouwanttogivetoyourfile.txt", userInput);
This reads the file and then outputs it
string readText = File.ReadAllText("thenameyouwanttogivetoyourfile.txt");
Console.WriteLine(readText);
Thats how you create a new file. To overwrite a file that already exists, you do the same thing but with the keyword Create(). This creates or overwrites a file and if you want to replace the contents of one file with another, use Replace().
For futher help try this link:
https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.8
This is the code i use to create the text file.
System.IO.File.WriteAllLines(#"C:\Users\****\Desktop\File.txt", array);
How do I append text to an existing file?
The other answers have shown you how to append a single string to a text file. If you naturally have a collection of lines, however, you probably want File.AppendAllLines:
File.AppendAllLines(#"C:\Users\****\Desktop\File.txt", array);
You want File.AppendAllLines:
Appends lines to a file, and then closes the file. If the specified
file does not exist, this method creates a file, writes the specified
lines to the file, and then closes the file.
File.AppendAllLines(#"C:\Users\****\Desktop\Passwords.txt", array);
Like this:
using (StreamWriter sw = File.AppendText(#"C:\Users\****\Desktop\Passwords.txt"))
{
sw.WriteLine(UsernamesAndPass);
}
Ref: https://msdn.microsoft.com/en-us/library/system.io.file.appendtext(v=vs.110).aspx
I am doing a project on my own. I want to check if the entered text in a TextBox control matches a list of words entered in notepad.
My questions are:
How to attach notepad to my project?
How to check the words against the list of the words found in notepad?
Please guide me in the right direction.
//This code first read the words in text file one by one,
//which are save in notepad file like one word per line
int aCounter = 0; string aWordInTextFile;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\notePadFile.txt");
while((aWordInTextFile = file.ReadLine()) != null){
Console.WriteLine (aWordInTextFile);
if(textbox.text == aWordInTextFile){
messagebox.show("String Match, found a string in notepad file");
}
aCounter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
You dont need to attach your notepad to project, just open it using System.Io classes and read it all and check your string using comparison methods.
This question seems to have been asked a million times around the web, but I cannot find an answer which will work for me.
Basically, I have a CSV file which has a number of columns (say two). The program goes through each row in the CSV file, taking the first column value, then asks the user for the value to be placed in the second column. This is done on a handheld running Windows 6. I am developing using C#.
It seems a simple thing to do. But I cant seem to add text to a line.
I cant use OleDb, as System.Data.Oledb isnt in the .Net version I am using. I could use another CSV file, and when they complete each line, it writes it to another CSV file. But the problems with that are - The file thats produced at the end needs to contain EVERY line (so what if they pull the batterys out half way). And what if they go back, to continue doing this another time, how will the program know where to start back from.
For every row, open the output file, append the new row to it, and then close the output file. To restart, count the number of rows in the existing output file from the previous run, which will give you your starting in the input file (i.e., skip that number of rows in the input file).
Edit: right at the start, use System.IO.File.Copy to copy the input file to the output file, so you have all the file in case of failure. Now open the input file, read a line, convert it, use File.ReadAllLines to read ALL of the output file into an array, replace the line you have changed at the right index in the array, then use File.WriteAllLines to write out the new output file.
Something like this:
string inputFileName = ""; // Use a sensible file name.
string outputFileName = ""; // Use a sensible file name.
File.Copy(inputFileName, outputFileName, true);
using (StreamReader reader = new StreamReader(inputFileName))
{
string line = null;
int inputLinesIndex = 0;
while ((line = reader.ReadLine()) != null)
{
string convertedLine = ConvertLine(line);
string[] outputFileLines = File.ReadAllLines(outputFileName);
if (inputLinesIndex < outputFileLines.Length)
{
outputFileLines[inputLinesIndex] = convertedLine;
File.WriteAllLines(outputFileName, outputFileLines);
}
inputLinesIndex++;
}
}
I want to insert the data at some positions in the text file without actually overwriting on the existing data. I have two text file. "one.txt" file have 1000 lines, "two.txt" file have 10000 lines. I want to read "one.txt" file content and insert into first 1000 lines of "two.txt" file content(Append the content of "one.txt" to the beginning of "two.txt").
Criteria:
Minimum code .
Less Memory consumption(irrespective of programming language )
Performance (will be considered based on size of the file).
just open up a streamreader for the first file, and a stream writer (in append mode) for the second file. As your reading the first 1000 lines from the first file, insert them into the second.
Something like this:
StreamReader sr = new StreamReader("one.txt");
StreamWriter sw = new StreamWriter("two.txt", true); //true for append
index i = 0;
while (i < 1000) {
sw.WriteLine(sr.ReadLine());
i++;
}
You may want to check for end of file on the StreamReader, but this will give you the general idea....
Based on the new information in OP:
You can use this same type of method, but just create a brand new file, reading the data from the first file, followed by the data from the second file. Once it's inside the new file, replace the original "two.txt".
If you're not limited to c# you can just do the following from a windows command line:
copy one.txt + two.txt three.txt
This would create the file you want, but it would be called three.txt. If you must have it in two.txt, you could simply rename two.txt to something else first and then do the copy append with two.txt as the third parm.
If you only have to do this once, here is some code that will do what you want. I did not compile this, but I believe there are no issues.
string[] linesOne = File.ReadAllLines(pathToFile1);
string[] linesTwo = File.ReadAllLines(pathToFile2);
List<string> result = new List<string>();
for(int i=0;i<1000;i++)
{
result.Add(linesOne[i]);
}
result.AddRange(linesTwo);
File.WriteAllLines(pathToFile2, result);
Hope this gets you started.
Bob