0

I wanted to delete 2 last lines of an text file.

I've found this how to delete last line in forum. I get an error of overload of converting string to string[] . After a look I've found this wich advise to use of array. But both does not work with me ?!

Finally I've wrote that

 string [] lines = File.ReadAllLines(PublicVariables.AddedFileName);
        string [] newlines = new string[lines.Length-2];
        for (int i=0; i<lines.Length - 2; ++i)
            newlines[i] = lines[i];
        File.WriteAllLines(PublicVariables.AddedFileName, newlines);

Why when I use those given exemples don't work with me ? Should I add something ?

I have those as using:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; 
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Net.NetworkInformation;
using System.IO;
using System.Threading;
using System.Diagnostics;
Community
  • 1
  • 1
Ismail Gunes
  • 548
  • 1
  • 9
  • 24

4 Answers4

1

Make sure that PublicVariables.AddedFileName is of type string.

To reduce the possibility of bugs in your code and improve performance for larger files, you can replace your code by this:

string[] lines = File.ReadAllLines(PublicVariables.AddedFileName);
File.WriteAllLines(PublicVariables.AddedFileName, 
    lines.Take(lines.Length - 2));

lines.Take uses Linq to enumerate of the array until the second to last element is reached, this makes it possible to not copy the array in memory (and not have it in memory twice).

Bas
  • 26,772
  • 8
  • 53
  • 86
  • Nice Linq solution! :) – Ashigore Dec 03 '13 at 16:43
  • Yes it's a static string. What you wrote exists in the links that I've wrote. But I can't use what you wrote. debugger gives "The best overloaded method match for 'System.IO.File.WriteAllLines(string, string[])' has some invalid arguments" error. – Ismail Gunes Dec 03 '13 at 16:50
  • The trouble begins when I add after lines Take (what I want to use :)) – Ismail Gunes Dec 03 '13 at 16:54
1

You can use ArrayResize to make things simplier:

using System;
using System.IO;
using System.Text;

string [] lines = File.ReadAllLines(PublicVariables.AddedFileName);
Array.Resize(ref lines , lines.Length-2);

File.WriteAllLines(PublicVariables.AddedFileName, lines);
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
0

probably the handle of the file is locked and you are trying to access the some file with different handle try to change the name of the file when you write for example

 File.WriteAllLines(PublicVariables.AddedFileName+"1", newlines);
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

There is nothing wrong with the code you have posted that could cause the error you stated so the problem must lie elsewhere, however, this code should be much faster:

string[] lines = File.ReadAllLines(PublicVariables.AddedFileName);
string[] newlines = new string[lines.Length - 2];
Buffer.BlockCopy(lines, 0, newlines, 0, newlines.Length);
File.WriteAllLines(PublicVariables.AddedFileName, newlines);
Ashigore
  • 4,618
  • 1
  • 19
  • 39
  • Sorry this code is working which is not working with me are the codes in the links. This code I've wrote using the links using a part of the codes of links. The aruments as take does not work – Ismail Gunes Dec 03 '13 at 16:49