Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've created a c# .net(4.5) command line application. This application does nothing, but stay open:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("started");
        Console.ReadLine();
    }
}

If i navigate to this file in explorer, i can rename it with F2 while the program is running.

If i try to delete it, a message is shown: enter image description here

I can understand that it is not possible to delete a loaded assembly. Move the loaded assembly from one folder to another doesn't work either.

But why can it be renamed?

Is this intended? Is there a use case to rename an existing/loaded assembly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

Files are represented in the file system by two major structures. First is the directory entry, it stores metadata about the file. Like the file name, size, attributes and time stamps. Next is the chain of clusters that store the file data.

Loading an assembly causes a memory mapped file to be created for the assembly. An optimization, it ensures that you only pay for the parts of the assembly you'll actually use. The file data won't be read into RAM until it is actually needed. A core feature of a demand-paged virtual memory operating system like Windows.

The memory mapped file puts a lock on the file to ensure the file data cannot be changed by another process. That would be disastrous, the data in RAM would not match the data in the file anymore. Locks in Windows only protect the file data, not the metadata for the file. Or in other words, you can still modify the directory entry for the file, as long as that doesn't also modify the file data.

So renaming the file is not a problem, that only modifies the directory entry. Even moving the file from one directory to another is not a problem, that only changes the location of the directory entry, it doesn't modify the file data as long as file is large enough to require clusters. Moving the file from one drive to another is a problem since that requires a copy that also deletes the original file data. Deleting the file is not possible, that also deletes the file data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...