为什么重命名加载.NET程序集的工作?重命名、加载、程序、工作

2023-09-06 16:17:11 作者:帅气的简单男

我已经创建了一个C#.NET(4.5)命令行应用程序。 该应用什么也不做,但保持开放:

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();
    }
}

如果我定位到浏览器这个文件,我可以在程序运行时用F2重命名。

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:

我可以理解,这是不可能删除加载的程序集。将加载的程序集从一个文件夹到另一个也不行。

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?

推荐答案

文件重新$ P $。首先是目录条目的,它存储元数据文件。像的文件名,大小,属性和时间标记。下一个是存储该文件的数据的簇的链

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.

加载一个组件,导致为大会创造了一个内存映射文件。一个优化的,它确保您只需支付你实际使用的组件的部分。文件数据不会被读取到RAM,直到它被实际需要。需求分页虚拟内存的操作系统如Windows的核心功能。

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.

映射文件的存储器将一个锁定上的文件,以确保该文件的数据的不能被另一个进程改变。那将是灾难性的,在RAM中的数据将不匹配的文件中的数据了。在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.