Using the following code:
string lines = "";
using (StreamReader sr = new StreamReader(#"file.txt"))
{
lines = sr.ReadLine();
}
using (StreamWriter writer = new StreamWriter(#"file.txt"))
{
writer.Write(lines); // Change this to skip the first line
}
How can I make it rewrite everything EXCEPT the first line?
Maybe you can try this:
var lines = File.ReadLines("file.txt").Skip(1).ToList();
File.WriteAllLines("file.txt",lines);
It will write all the lines to your file except the first line and replace your file content.So basically it will remove the first line from your file.
var allLinesExceptFirstOne = System.IO.File.ReadAllLines(filename).Skip(1).ToList();
Related
I am reading a file that contains other file paths and i am putting it into a StringReader:
var inputReader = new StreamReader(input);
var reader = new StringReader(inputReader.ReadToEnd());
When i debug the StreamReader.ReadToEnd() and look at the whole text everything seems right. The paths have this form: C:\\User\\ ....
However when i now read each line in the file with:
string line = reader.ReadLine();
The paths have the following form: C:\\\\User\\\\ ....
Any suggestions on how to fix that? Thank you in advance!
I´m developping with c# Interop Word, and I need to know when a page ends, because I´m creating a Word Table. Rows number isn´t always the same,and if the page doesn´t end, then I have to insert a break page.
How can I do that ?
If you're using Streamreader to read your word file, you can do this:
string path = #"C:\..."; // the path of your word file
Streamreader sr = new Streamreader(path);
while (!sr.EndOfStream) {
// what you want to do
{
sr.Close();
If you want to know at what line your file ends, you just create a count value and when your file ends, you check the value of the count and you have it:
string path = #"C:\..."; // the path of your word file
int count = 0;
Streamreader sr = new Streamreader(path);
while (!sr.EndOfStream) {
// what you want to do
count++;
{
sr.Close();
I want to copy data from text file to word file. I already have tried it with different alternatives like string array, StringBuilder and StreamReader using Interop which works good, but it takes too much time. It would really be thankful if anyone can suggest me with a better one. Been through many forms on the web, but couldn't find.
FYI: My text file contains more than 1,00,000 lines.
This is one of which I have tried:
string[] lines = File.ReadAllLines(path); //path is text file path
var doc = new MSWord.Document();
foreach (string lin in lines)
{
doc.Content.Text += lin.ToString();
}
doc.Save();
Well, this works good but takes a lot of time and also sometimes throws an error like:
Unhandled Exception: System.Runtime.InteropServices.COMException: Word has encountered a problem.
static void Main(string[] args)
{
Word.Application wordApp = new Word.Application();
Word.Document wordDoc = wordApp.Documents.Add();
Stopwatch sw = Stopwatch.StartNew();
System.Console.WriteLine("Starting");
string path = #"C:\";
StringBuilder stringBuilder = new StringBuilder();
using (FileStream fs = File.Open(path + "\\big.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
wordDoc.Content.Text = sr.ReadToEnd();
wordDoc.SaveAs("big.docx");
}
sw.Stop();
System.Console.WriteLine($"Complete Time :{sw.ElapsedMilliseconds}");
System.Console.ReadKey();
}
Output :
Starting
Complete Time :5556
Or You can use Parallel :
using (StreamReader sr = new StreamReader(bs))
{
Parallel.ForEach(sr.ReadToEnd(), i=>
{
stringBuilder.Append(i);
});
wordDoc.Content.Text = stringBuilder.ToString();
wordDoc.SaveAs(path + "\\big3.docx");
}
Output:
Starting
Complete Time :2587
Microsoft Word can read text files - so why not read the text file into an Interop Word Document & then convert by using one of the SaveAs methods.
I tested with a 34Mb, 1000000 line text file - the result was a 22Mb DOCX file:
MSWord.Application appAC = new MSWord.Application();
MSWord.Document doc = appAC.Documents.Open("TestRead.txt");
doc.SaveAs2(FileName:"TestSave", FileFormat:WdSaveFormat.wdFormatDocumentDefault);
doc.Close();
appAC.Quit();
Note that Microsoft states a maximum document size of 32MB - the text file exceeded this, but the resulting DOCX file was smaller - your exception maybe related to the size of the final file.
I am using CSVWriter to write to csv by using the following link:
How to create csv with header and footer using LinqtoCsv or anything better in C#
But for some reason i am getting an empty line created at the end. Can anyone help me what am i doing wrong or how can i avoid getting an empty line?
Here is the code to create csv using csvwriter:
using (var writer = File.AppendText(outputCsv))
{
var csv = new CsvWriter(writer);
csv.Configuration.RegisterClassMap<CustomMap>();
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.QuoteNoFields = true;
csv.Configuration.Delimiter = "|";
csv.WriteRecords(asRunLogs.ToList());
csv.WriteRecord(new CSVFooterRecord()
{
FooterText =
$"TRAILER:{"qsaam_promos.csv"}|{DateTime.Now.ToString("ddMMyyHHmm")}|{asRunLogs.Count}"
});
}
When i open the file in Notepad++, i can see an empty line at the end.
Appreciate help with regard to this.
Thanks
You could do
writer.Write($"TRAILER:{"qsaam_promos.csv"}|{DateTime.Now.ToString("ddMMyyHHmm")}|{asRunLogs.Count}");
writer.Flush();
instead of
csv.WriteRecord(new CSVFooterRecord()
{
FooterText =
$"TRAILER:{"qsaam_promos.csv"}|{DateTime.Now.ToString("ddMMyyHHmm")}|{asRunLogs.Count}"
});
I tried to write to CSV file using CsvHelper in C#.
This is the link to the library http://joshclose.github.io/CsvHelper/
I used the code in web site.
Here is my code:
var csv = new CsvWriter(writer);
csv.Configuration.Encoding = Encoding.UTF8;
foreach (var value in valuess)
{
csv.WriteRecord(value);
}
It writes only a part of data to csv file.
Last rows were missing.
Could you please help with this.
You need to flush the stream. The Using statement will flush when out of scope.
using (TextWriter writer = new StreamWriter(#"C:\test.csv", false, System.Text.Encoding.UTF8))
{
var csv = new CsvWriter(writer);
csv.WriteRecords(values); // where values implements IEnumerable
}
when, I added this code after the loop code is working well
var csv = new CsvWriter(writer);
csv.Configuration.Encoding = Encoding.UTF8;
foreach (var value in valuess)
{
csv.WriteRecord(value);
}
writer.Close();
The problem occurred because I did not close the Connection
Assuming that writer is some kind of TextWriter, you should add a call to flush the contents before closing the writer:
writer.Flush()
If the last lines are missing, this is the most likely reason.
Adding to #greg's answer:
using (var sr = new StreamWriter(#"C:\out.csv", false, Encoding.UTF8)) {
using (var csv = new CsvWriter(sr)) {
csv.WriteRecords(values);
}
}