工作 C# 示例:编写 &Win7 64位下读取NTFS备用数据流 [英] Working C# Example: Writing & Reading NTFS Alternate Data Stream Under Win7 64 bit

查看:22
本文介绍了工作 C# 示例:编写 &Win7 64位下读取NTFS备用数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用备用数据流为我正在构建的数据库应用程序中引用的文件存储一些同步信息.

I'd like to be able to use an alternate data stream to store some synchronization information for files that are referenced in a database application I'm building.

然而,我在网上找到的每一种方法都以某种方式失败了.没有 Win32 API 编程经验我不确定为什么会发生故障.在某些情况下,方法调用似乎成功了,只是没有创建备用流(即,我以后无法从备用文件中读取,也无法看到 AlternateStreamViewer).在其他情况下,写入成功,但读取失败,因为调用 CreateFile() 导致无效的 SafeFileHandle.

However, every approach I've found on the web has failed in one way or another. Not being experienced at Win32 API programming I'm not sure why the failures are occurring. In some cases the method calls appear to succeed, only no alternate stream is created (i.e., I can't later read from the alternate file, nor can AlternateStreamViewer see it). In other cases the write succeeds, but the read fails because calling CreateFile() results in an invalid SafeFileHandle.

此时我只想找到一些工作代码.或者被告知你不能从这里到达那里".在这种情况下,我将使用另一个不太吸引人的选项(例如,在文件名本身中编码同步/参考信息,并希望没有人更改文件名).

At this point I'd just like to find some working code. Or be advised that "ya can't get there from here". In which case I'll use another, less attractive option (e.g., encode the sync/reference information in the filename itself, and hope no one ever changes a file name).

我了解我尝试过哪些方法的要求.但是有很多(我花了几个小时),我真的很想了解我没有尝试过的产品.

I understand the request for what approaches I tried. But there were quite a few (I spent hours on this), and I was really interested in learning about offerings I hadn't tried.

但是,您说得对,它会有所帮助,因为事实证明我错误地使用了其中一种方法——下面提到的 Trinet 方法.我认为问题在于我还没有创建我试图为其保存备用流的基本"文件.

However, you're right that it would've helped, as it turns out I was using one of the approaches -- the Trinet one referred to below -- incorrectly. I think the problem was that I hadn't yet created the "base" file for which I was trying to save an alternate stream.

以下是我尝试过的代码库的链接:

Here are links to the code bases I tried:

推荐答案

我使用这个库在 Windows 7 x64 上取得了成功:

I've had success on Windows 7 x64 using this library:

https://github.com/hubkey/Trinet.Core.IO.Ntfs

我找不到我的旧代码,而且我收藏的文档页面目前已关闭,所以如果您仍然遇到问题,我会在备份后尝试发布一些代码.

I can't find my old code, and the documentation page I have bookmarked is down at the moment, so I'll try to post some code when it's back up if you're still having problems.

显然它很简单:

using Trinet.Core.IO.Ntfs;

var fileInfo = new FileInfo(@"C:\path\to\file.dat");
if (AlternateDataStreamExists("MyStreamName"))
{
    var alternateStream = fileInfo.GetAlternateDataStream("MyStreamName").OpenRead();
}
else
{
    var alternateStream = fileInfo.GetAlternateDataStream("MyStreamName").OpenWrite();
}

这篇关于工作 C# 示例:编写 &Win7 64位下读取NTFS备用数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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