0

My program goes through all files in a folder, reads them, and without altering their information moves them to another location under a different name. However I cannot use the File.Move method because I get the following IOException:

The process cannot access the file because it is being used by another process.

This is how I am reading the file and adding all its lines to a List<string>:

List<string> lines = null;
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (var sr = new StreamReader(fs, Encoding.Default))
    {
        lines = new List<string>();
        while (!sr.EndOfStream)
            lines.Add(sr.ReadLine());
    }

And this is the function with which I move the file:

public static bool ArchiveFile(string filePath, string archiveFolderLocation)
{
    if (!Directory.Exists(archiveFolderLocation))
        Directory.CreateDirectory(archiveFolderLocation);
    try
    {
        string timestamp = string.Format("{0:yyyy-MM-dd HHmmss}", DateTime.Now);
        string newFileName = Path.GetFileNameWithoutExtension(filePath) + " " + timestamp;
        string destination = string.Format("{0}\\{1}{2}", archiveFolderLocation, newFileName, Path.GetExtension(filePath));
        File.Move(filePath, destination);
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

I thought using the using statement is supposed to garbage-collect and release the file after being used. How can I release the file so I can move it and why my file stays locked?

Solved:

Got it. Somewhere between these two calls I was opening a TextReaderobject without disposing it.

disasterkid
  • 6,948
  • 25
  • 94
  • 179

2 Answers2

0

I thought using the using statement is supposed to garbage-collect and release the file after being used. How can I release the file so I can move it and why my file stays locked?

Not really. Using statement is nothing but :

try { var resource = new SomeResource(); } 

finally { resource.Dispose(); // which is not GC.Collect(); }

It works fine so it looks like your file is opened from some other place in your code...

P.S. By the way you can just do:

List<string> lines = File.ReadAllLines().ToList();

Fabjan
  • 13,506
  • 4
  • 25
  • 52
-2

You could use:

string dpath = "D:\\Destination\\";
            string spath = "D:\\Source";
            string[] flist = Directory.GetFiles(spath);
            foreach (string item in flist)
            {
                File.Move(item, dpath + new FileInfo(item).Name);
            }

Replace D:\\Source & D:\\Destination\\ with the required source and destination paths, respectively.

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • Why `dynamic flist`? I'd understand `var flist`, but `dymanic` seems inappropriate here, where you're *guaranteed* you'll get a `string[]` and only use it as that. – Corak Jul 14 '15 at 13:07
  • yeah you're right. i don't know much of c#. i wrote the program in vb.net. then using the Telerik Code Converter converted it into C#. it converted 'dim' to 'dynamic'. Now fixed. – Fᴀʀʜᴀɴ Aɴᴀᴍ Jul 14 '15 at 13:08
  • If you're unsure of the answer, it might be better suited as a comment, as opposed to an answer. Telerik's code converter works okay most of the time, but has the tendency to occasionally produce weird or in some cases, completely broken code. – user2366842 Jul 14 '15 at 13:10
  • but i tested this code and it works perfect. test it for yourself if you want – Fᴀʀʜᴀɴ Aɴᴀᴍ Jul 14 '15 at 13:12