Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

c# - Why does rename a loaded .net assembly work?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...