且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取在网络驱动器上打开文件的用户的用户名-Microsoft Office Style

更新时间:2021-10-05 23:30:35

Office使用一种非常简单的技术,我将以.NET术语进行讨论.每当Office应用程序使用FileShare.None打开文档文件时,它还会创建一个隐藏的锁定文件",其名称基于该文档文件(例如,附加".lockfile").然后将Environment.UserDomainName写入该文件.该文件是使用FileOptions.DeleteOnClose和FileShare.Read创建的,只要打开了文档文件,便保持打开状态.

Office uses a very simple technique, I'll talk about it in .NET terms. Whenever an Office app opens a document file, using FileShare.None, it also creates a hidden "lock-file" with a name that's based on the document file (say, with ".lockfile" appended). And writes Environment.UserDomainName into that file. The file is created with FileOptions.DeleteOnClose and FileShare.Read and kept open as long as the document file is open.

当再次关闭文档时,它会关闭锁定文件.使用FileOptions.DeleteOnClose可以确保即使程序发生轰炸,锁定文件也不会消失.

It closes the lock-file when the document is closed again. Using FileOptions.DeleteOnClose ensures that the lock-file disappears even when the program bombs.

打开文件时,出现锁定冲突,它将继续寻找锁定文件并读取用户名.轻松自在,轻松实现自己.但是,当然只有在打开文件的是一个特定的应用程序时才能起作用.

When opening the file produces a locking violation, it goes looking for the lock-file and reads the user name. Easy peasy, simple to implement yourself. But can of course only work if it is one particular app that opens the file.