Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to remove text from a specific line in the file that i have created "Test.txt".

Here is the text written in Test.txt:
1
2
3
4
5
6
7
8
9
10

Now what i want to happen is when i'm going to remove 1-5, the numbers 6 - 10 should replace the line for 1-5. Like this:
6
7
8
9
10

But when i tried the code it gave me this output:





6
7
8
9
10

As you can see the above output, that is the unwanted space that i'm talking about.

What I have tried:

Here is the code that I have tried:

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
class Program2
{
    static void Main()
    {
        lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 1);
        lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 2);
        lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 3);
        lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 4);
        lineChanger("", @"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", 5);
    }

    static void lineChanger(string newText, string fileName, int line_to_edit)
    {
        string[] arrLine = File.ReadAllLines(fileName);
        arrLine[line_to_edit - 1] = newText;
        File.WriteAllLines(fileName, arrLine);
    }
}
}


Also please feel free to suggest any proper techniques and kindly include actual examples.
Posted
Updated 6-Feb-17 3:08am
Comments
[no name] 6-Feb-17 8:46am    
So only write back to the file elements of the array that actually contain something other than an empty string.

1 solution

Using a list instead of an array makes it easier.

C#
static void lineChanger(string newText, string fileName, int line_to_edit)
{
    List<string> arrLine = File.ReadAllLines(fileName).ToList();
    if (arrLine.Count <= line_to_edit)
    { 
        if (string.IsNullOrEmpty(newText))
        {
            arrLine.RemoveAt(line_to_edit - 1);
        }
        else
        {
            arrLine[line_to_edit-1] = newText;
        }
        File.WriteAllLines(fileName, arrLine.ToArray());
    }
    else
    {
        throw new Exception(string.Format("File does not contain {0} lines. Operation aborted.", line_to_edit));
    }
}
 
Share this answer
 
v3
Comments
[no name] 7-Feb-17 8:59am    
This code is not working. It is giving me this exception: An unhandled exception of type 'System.Exception' occurred in TestCode.exe

Additional information: File does not contain 1 lines. Operation aborted.

How to fix this?
#realJSOP 7-Feb-17 9:52am    
How to fix it? Hire a programmer.

Have you though of - oh, I don't know - using the debugger to see why it's throwing the exception?
[no name] 7-Feb-17 10:15am    
how rude..
jimmson 7-Feb-17 10:28am    
People here are trying to help programmers, and not doing their job for free..
[no name] 7-Feb-17 10:50am    
you suggested a solution and it did not work. Isn't it just right that you fix your solution so you can help programmers?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900