Getting exception when trying to delete a line from a text file. How can i solve it?

Simon Gamlieli

In the top of form1:

StreamWriter recentfiles;
string[] lines;
string line = null;
int line_number = 0;
string RecentFiles = "";

In the constructor:

RecentFiles = @"e:\RecentFiles.txt";
if (!File.Exists(RecentFiles))
{
    recentfiles = new StreamWriter(RecentFiles);
    recentfiles.Close();
}
else
{
    lines = File.ReadAllLines(RecentFiles);
    items = File
            .ReadLines(@"e:\RecentFiles.txt")
            .Select(line => new ToolStripMenuItem()
            {
              Text = line
            })
            .ToArray();
}
for (int i = 0; i < items.Length; i++)
{
    if (!File.Exists(items[i].Text))
    {
        using (StreamReader reader = new StreamReader(RecentFiles))
        {
            using (StreamWriter writer = new StreamWriter(RecentFiles))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    line_number++;

                    if (line_number == i)
                        continue;

                    writer.WriteLine(line);
                }
            }
        }
    }
}

In the text file RecentFiles for example in the first line I have:

d:\test.txt

In the second line:

e:\test\test.txt

What I want to do is that if one of the files in the text file is not exist remove it delete it from the RecentFiles.txt

But I'm getting exception on the line:

using (StreamWriter writer = new StreamWriter(RecentFiles))

The process cannot access the file 'e:\RecentFiles.txt' because it is being used by another process

How can i solve it ?

Garr Godfrey

just make it better:

lines = File.ReadAllLines(RecentFiles);
List< ToolStripMenuItem > items = new List< ToolStripMenuItem >();
using (StreamWriter writer = new StreamWriter(RecentFiles))
{
    for (int i = 0; i < lines.Length; i++)
    {
        if (File.Exists(lines[i])) {
            writer.WriteLine(lines[i]);
            items.Add(new ToolStripMenuItem()
              {
                Text = lines[i]
              });
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I delete a multi-line record from a text file?

From Dev

How can I delete a multi-line record from a text file?

From Dev

How do I add / delete a line from a text file?

From Dev

How can i delete space from text file and replace it semicolon?

From Dev

How can I delete all unordered lines from a text file?

From Dev

How can i delete last column from my text file

From Dev

How can i delete from a text file these special characters

From Dev

Trying to delete Android Realm file, but getting exception

From Dev

When using WebClient to download files i'm getting exception does not support concurrent I/O operations how can i solve it?

From Dev

How can I get a specific line from a text file?

From Dev

How can I display a random line from a text file?

From Dev

How can I place an line text from a file into an textbox?

From Dev

How would I solve the errors which I am getting when trying to use android project from existing code?

From Dev

Getting a "The following content types are stale and need to be deleted" when trying to do a migrate. What does this mean, and how can I solve it?

From Dev

How can I replace a line in a text file?

From Dev

How can I solve error throwed when I'm trying to download Source code of Jaspersoft Studio from SVN?

From Java

How do I preserve line breaks when getting text from a textarea?

From Dev

How can i read from a text file line by line and add those strings to an array?

From Dev

I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it?

From Dev

How can I delete the 0-8th and then the second from the top line in a file [Python]

From Dev

How can I delete the 0-8th and then the second from the top line in a file [Python]

From Dev

How can I delete everything after a word from multiple line in a file

From Dev

How to delete a specific line from a text file in Delphi

From Dev

how to delete a line from text file that is saved inside a string variable

From Dev

How can I read and replace Strings line by line in a Text File?

From Dev

I am getting null pointer exception when I an trying to putExtra from intent

From Dev

Trying to create a grads script and getting error " Expected end of statement " how can I solve it?

From Dev

How can I delete multiple random lines from a text file using sed?

From Dev

How can I delete a fixed part of some lines from a text file?

Related Related

  1. 1

    How can I delete a multi-line record from a text file?

  2. 2

    How can I delete a multi-line record from a text file?

  3. 3

    How do I add / delete a line from a text file?

  4. 4

    How can i delete space from text file and replace it semicolon?

  5. 5

    How can I delete all unordered lines from a text file?

  6. 6

    How can i delete last column from my text file

  7. 7

    How can i delete from a text file these special characters

  8. 8

    Trying to delete Android Realm file, but getting exception

  9. 9

    When using WebClient to download files i'm getting exception does not support concurrent I/O operations how can i solve it?

  10. 10

    How can I get a specific line from a text file?

  11. 11

    How can I display a random line from a text file?

  12. 12

    How can I place an line text from a file into an textbox?

  13. 13

    How would I solve the errors which I am getting when trying to use android project from existing code?

  14. 14

    Getting a "The following content types are stale and need to be deleted" when trying to do a migrate. What does this mean, and how can I solve it?

  15. 15

    How can I replace a line in a text file?

  16. 16

    How can I solve error throwed when I'm trying to download Source code of Jaspersoft Studio from SVN?

  17. 17

    How do I preserve line breaks when getting text from a textarea?

  18. 18

    How can i read from a text file line by line and add those strings to an array?

  19. 19

    I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it?

  20. 20

    How can I delete the 0-8th and then the second from the top line in a file [Python]

  21. 21

    How can I delete the 0-8th and then the second from the top line in a file [Python]

  22. 22

    How can I delete everything after a word from multiple line in a file

  23. 23

    How to delete a specific line from a text file in Delphi

  24. 24

    how to delete a line from text file that is saved inside a string variable

  25. 25

    How can I read and replace Strings line by line in a Text File?

  26. 26

    I am getting null pointer exception when I an trying to putExtra from intent

  27. 27

    Trying to create a grads script and getting error " Expected end of statement " how can I solve it?

  28. 28

    How can I delete multiple random lines from a text file using sed?

  29. 29

    How can I delete a fixed part of some lines from a text file?

HotTag

Archive