简体   繁体   中英

C# can't delete file after streamreader use

I can't seem to be able to delete files after a streamreader use, with a

"file can't be accessed because file is in use"

error in C#.

I may miss something but I don't know what, here is the code :

fileEntries = from fullFilename
    in Directory.EnumerateFiles(@"Data\csv\pending")
    select Path.GetFileName(fullFilename);

i = 1;

foreach (string file in fileEntries)
{
    if(i == 1)
    {
        folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Data\csv\done";

        using (System.IO.FileStream fs = System.IO.File.Create(folder + @"\create-user.csv"))
        {

        }

        using (System.IO.StreamWriter files = new System.IO.StreamWriter(folder + @"\create-user.csv", true))
        {
            files.WriteLine(",; prenom; nom; username; pasword; email; question; reponse; GroupID");
        }

        string curfile = @"\create-user-archive.csv";
        if(!(File.Exists(folder + curfile)))
        {
            using (System.IO.StreamWriter files = new System.IO.StreamWriter(folder + @"\create-user-archive.csv", true))
            {
                files.WriteLine(",; prenom; nom; username; pasword; email; question; reponse; GroupID");
            }
        }
    }


    folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Data\csv\pending";
    sb = new StringBuilder();

    filef = new System.IO.StreamReader(folder + @"\create-user-" + i + ".csv");
    line = filef.ReadLine();

    while ((line = filef.ReadLine()) != null)
    {
        sb = new StringBuilder();
        sb.AppendLine(line.Substring(0, line.Length));
        folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Data\csv\done";
        using (System.IO.StreamWriter files = new System.IO.StreamWriter(folder + @"\create-user.csv", true))
        {
            files.WriteLine(",; " + sb.ToString().Split(';')[1] + ";" + sb.ToString().Split(';')[2] + ";" + sb.ToString().Split(';')[1] + "." + sb.ToString().Split(';')[2] + ";" + GenerateToken(6) + ";" + sb.ToString().Split(';')[3] + ";" + "1" + ";" + "1");
        }

        folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Data\csv\done";
        using (System.IO.StreamWriter files = new System.IO.StreamWriter(folder + @"\create-user-archive.csv", true))
        {
            files.WriteLine(",; " + sb.ToString().Split(';')[1] + ";" + sb.ToString().Split(';')[2] + ";" + sb.ToString().Split(';')[1] + "." + sb.ToString().Split(';')[2] + ";" + GenerateToken(6) + ";" + sb.ToString().Split(';')[3] + ";" + "1" + ";" + "1");
        }
    }
    i++;
    sourceFile = System.IO.Path.Combine(@"Data\csv\pending", file);
    File.Delete(sourceFile);
}

shouldn't the file stop being in use after the streamreader is finished? I tried using a function that waits until the file is unlocked to delete the file, but it is infinite, which means there is a never ending process that I must stop, but I don't see which one.

You need to close filef.

Wrapping the code in a using statement will automatically close the reader

using ( System.IO.StreamReader filef = new System.IO.StreamReader(folder + @"\create-user-" + i + ".csv") {
    ....yourcodehere
}

Alternatively, call filef.Close() when you are done with it (before you delete the file)

You have to close the streams you create to dispose the system resources. You can either use the Close method or the using pattern, as the classes implemented IDisposable interface. I would recommend you to the second option.

May have a look to this post: https://stackoverflow.com/a/707339/6244709

You will need to call the following;

filef.Close();

This would go before your delete;

  while ((line = filef.ReadLine()) != null)
            {
                sb = new StringBuilder();
                sb.AppendLine(line.Substring(0, line.Length));
                folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Data\csv\done";
                using (System.IO.StreamWriter files = new System.IO.StreamWriter(folder + @"\create-user.csv", true))
                {
                    files.WriteLine(",; " + sb.ToString().Split(';')[1] + ";" + sb.ToString().Split(';')[2] + ";" + sb.ToString().Split(';')[1] + "." + sb.ToString().Split(';')[2] + ";" + GenerateToken(6) + ";" + sb.ToString().Split(';')[3] + ";" + "1" + ";" + "1");
                }

                folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Data\csv\done";
                using (System.IO.StreamWriter files = new System.IO.StreamWriter(folder + @"\create-user-archive.csv", true))
                {
                    files.WriteLine(",; " + sb.ToString().Split(';')[1] + ";" + sb.ToString().Split(';')[2] + ";" + sb.ToString().Split(';')[1] + "." + sb.ToString().Split(';')[2] + ";" + GenerateToken(6) + ";" + sb.ToString().Split(';')[3] + ";" + "1" + ";" + "1");
                }
            }
            i++;
            sourceFile = System.IO.Path.Combine(@"Data\csv\pending", file);
            filef.Close();
            File.Delete(sourceFile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM