获取在网络驱动器上打开文件的用户的用户名-Microsoft Office Style [英] Get username of user who has file open on network drive - Microsoft Office Style

查看:225
本文介绍了获取在网络驱动器上打开文件的用户的用户名-Microsoft Office Style的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向使用 C#编写的在 Windows (主要是Windows 7)下运行的软件中添加用户友好的文件锁定. 通过在相应的过程中使文件保持打开"状态,我已经实现了文件锁定部分.我现在仍然要添加的是识别当前具有打开/锁定文件的用户.

I would like to add user friendly file locking to a software running under Windows (Windows 7 mostly), written in C#. I already achieved the file locking part, by keeping the files in use "open" in the corresponding process. What I now still would like to add is recognition of the user who has a file currently open/locked.

所访问的文件位于映射的网络驱动器上,供不同计算机上的不同用户使用. 当文件被锁定并且另一个人试图打开该文件时,他将面临一个对话框,类似于Microsoft Office程序中的正在使用文件"对话框.在那里,还会显示当前正在编辑文件的用户名.

The files being accessed lie on a mapped network drive, used by different users on different computers. When a file is locked and a second person tries to open the file, he should be confronted with a dialog, similar to the "File in use"-dialog from the Microsoft Office programs. There, also the name of the user, currently editing the file, is displayed.

我找到了解决方案,以找出具有特定文件打开状态的流程(使用该文件:

I found solutions to find out the processes, which have a certain file open (used this one: How do I find out which process is locking a file using .NET?) and I'm also able to read the name of the user who created this process out of it. However, when opening a locked file on a network drive, the username yielded by doing it like this, is always my own one, instead of the one from the user locking the file.

有人知道如何实现这一目标吗?我的意思是Microsoft Office也可以通过相同的用户权限在同一台PC上执行此操作.我只是想知道如何...

Does anyone have an idea how one could achieve this? I mean Microsoft Office somehow can do this on my same PC with the same user permissions, too. I just'd like to know how...

干杯!

推荐答案

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.

这篇关于获取在网络驱动器上打开文件的用户的用户名-Microsoft Office Style的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆