Manuel
Manuel

Reputation: 2203

Why can a file which is open in a stream not be renamed?

With the following piece of code, i read a file and keep the stream open.

    static void Main(string[] args)
    {
        FileStream fileStream = new FileStream(@"C:\temp\test1.txt", FileMode.Open);
        Console.ReadLine();
    }

If i now try to rename this file, the message "File in use" is shown. file in use

It looks like the rename of a file should be possible(on os level), even if it is in use: Why does rename a loaded .net assembly work?

Why can't a file be renamed when it is loaded in a stream by .net?

Upvotes: 1

Views: 1044

Answers (2)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64923

About assemblies, .NET should open the assembly using something like FileShare.ReadWrite so, even if it's loaded into the domain, any other process can write or rename the whole file.

AFAIK, .NET doesn't have a managed FileShare enum value for allowing or disallowing renaming the file share (something like FileShare.Rename), but Win32 should have thus assembly loading would be creating the file stream with a mask like FileShare.ReadWrite | FileShare.Rename (FileShare.Delete includes renaming but also removing the whole file...).

For demonstrating this, I've tried the following code:

    FileStream a = File.Open(@"C:\blah.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    a.Dispose();

While the file stream was opened and until it's disposed, I couldn't delete the whole file but I could rename it.

Note that assemblies are loaded into the AppDomain when they're needed, meaning that maybe you can write, delete or rename an assembly because it's not loaded yet.

Try to delete an assembly already loaded into the AppDomain: you can't (I've tried myself just now, the test code runs in a console app and I couldn't remove the executable while running it). Now try to rename it: it works.

Summary

It's all about how a file is opened and which file mask configuration was used when the file stream was opened: this is why your file can't be renamed but you can rename an already loaded assembly.

Modify your code like the next one and you'll be able to rename the whole file (even delete it!):

    FileStream fileStream = File.Open(@"C:\temp\test1.txt", FileMode.Open, FileShare.ReadWrite | FileShare.Delete);
    Console.ReadLine();

Upvotes: 3

eyossi
eyossi

Reputation: 4340

When you create a FileStream you don't load it's data to the memory. when you are seeking the stream, the file content is still read from the disk - a good thing to do when you need to read big files.

If all you want to do is to load the whole content to memory you can use File.ReadAllBytes. You can read more about it here: http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx.

Note: If you want to use a stream and not a byte array, you can use MemoryStream and pass the read file content to it.

Upvotes: 1

Related Questions