hi.
i want to know that how to delete or remove items from a file, keeping the file intact itself ???

Recommended Answers

All 3 Replies

Depends on what you mean by delete and what type of file.

For example, I have a text file with lines in it like these:

Line 1
Line 2
Line 3
Line 4

I delete "Line 2" do I get:

Line 1

Line 3
Line 4

or

Line 1
Line 3
Line 4

Line 1

Line 3
Line 4

Easy enough.

String[] lines = File.ReadAllLines("myfile.txt");
for (int i = 0; i < lines.Length; i++) {
    if (lines[i].StartsWith("The")) {
        lines[i] = String.Empty;
    }
}
File.WriteAllLines("myfile.txt", lines);

This will remove all the lines that begin with "The". You'll probably need to modify it to remove the lines you want removed :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.