使用Dokan实现虚拟驱动器。卡在 WriteFile 上
我正在使用 C# 和 Dokan 库编写文件系统,但卡在了 WriteFile 上。
public int WriteFile(
String filename,
Byte[] buffer,
ref uint writtenBytes,
long offset,
DokanFileInfo info)
{
try
{
FileStream fs = File.OpenWrite( GetFullPath(filename) , FileMode.Open );
fs.Seek(offset, SeekOrigin.Begin);
fs.Lock(offset, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
fs.Unlock(offset, buffer.Length);
writtenBytes = (uint)buffer.Length;
return 0;
}
catch (Exception e)
{
Console.WriteLine("FAIL WriteFile {0}", e.Message);
return -1;
}
}
当我运行应用程序并从 Dokan 的虚拟驱动器打开 txt 文件时,添加一些行并尝试保存它。我从记事本收到错误“参数不正确”。
在代码中,我在 File.OpenWrite() 调用上遇到异常。例外是 System.IO.IOException。消息:“该进程无法访问文件 foo.txt,因为它正在被另一个进程使用。”
- 文件只能通过记事本打开。
- 使用 Dokan 库提供的镜像示例可以观察到相同的行为
- ,我在清单中向我的程序添加了管理员权限,这没有帮助
Dokan 应该作为代理工作,允许调用用户定义的 WriteFile,对吧?如果写入被锁定,我该如何执行此操作?
请帮忙。也许您有使用 Dokan 的经验,或者有任何关于它不起作用的线索。
我正在使用 - Win 7 Pro x64 - 64位Dokan驱动程序 - 应用程序编译为 x86
I'm writing my filesystem using C# and Dokan library and got stuck on WriteFile.
public int WriteFile(
String filename,
Byte[] buffer,
ref uint writtenBytes,
long offset,
DokanFileInfo info)
{
try
{
FileStream fs = File.OpenWrite( GetFullPath(filename) , FileMode.Open );
fs.Seek(offset, SeekOrigin.Begin);
fs.Lock(offset, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
fs.Unlock(offset, buffer.Length);
writtenBytes = (uint)buffer.Length;
return 0;
}
catch (Exception e)
{
Console.WriteLine("FAIL WriteFile {0}", e.Message);
return -1;
}
}
When I run app and open txt file from Dokan's virtual drive, add some line and try to save it. I get error from notepad "The parameter is incorect".
In code I get exception on File.OpenWrite() call. The exception is System.IO.IOException. Message: "The process cannot access the file foo.txt because it is being used by another process."
- File is opened only by notepad.
- Same behavior can be observed with Mirror example delivered with Dokan library
- I added admin permissions to my program in the manifest, it didn't help
Dokan is supposed to work as proxy, allowing to call WriteFile defined by user, right? How can I do this if it's locked for writing?
Please help. Maybe you have any experience with Dokan or any clue why it's not working.
I'm using
- Win 7 Pro x64
- 64 bit Dokan driver
- App is compiled as x86
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在离开该方法之前,您必须在流上调用 Dispose。
这不是最好的解决方案。您应该在 CreateFile 中构造文件流,将其传递给 info.Context 并在 ReadFile 、 WriteFile 中使用它,并在 Cleanup 中调用 dispose 。
You must call Dispose on stream before leaving the method.
That's not the best solution. You shuld construct file stream in CreateFile , pass it to info.Context and the use it in ReadFile , WriteFile and call dispose on it in Cleanup.