I have a txt file that contains a list of files that i need to delete. so far this is what i have.
how do i add the delete statement. line.delete, sr.delete does not work.

class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("List.txt");
            String line;
            while ((line = sr.ReadLine()) != null)
            {

            }

        }

Recommended Answers

All 2 Replies

You have a few choices

1. Read the entire file in, and then write out the stuff you want
2. While reading through your file, output to a new file the stuff you want and move it.
3. Have a flag in your structure that says "ignore this line its for reuse" and then allow your code to reuse those records
4. Read through the file, and find the point you want, and then read the record after, and put it over the top of the old.. but this only works when you have fixed record sizes.
5. A semi combo of some of the above where you find the point in your file you want, read whats left, remove the bit you dont want and write back the results

I think you're a bit confused with files here. (Oh man if you knew how confused I still am, somethimes...)
A StreamReader is just that, a reader of a stream of bytes. It does not delete something, it has no Delete method. In your case it reads a line from a textfile. You are well on your way! What you now must do is use the static File class like so:
I am assuming the line you're reading is a path to a file.

File.Delete(line);

Hope this answers your question.

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.