将文件从 TrueCrypt 卷复制到剪贴板?

发布于 2024-10-15 17:18:46 字数 424 浏览 6 评论 0原文

我使用此代码将文件复制到剪贴板:

IDataObject data = new DataObject();
data.SetData(DataFormats.FileDrop, new string[] {@"X:\test.doc"});
MemoryStream memo = new MemoryStream(4);
byte[] bytes = new byte[] { (byte)(5), 0, 0, 0 };
memo.Write(bytes, 0, bytes.Length);
data.SetData("Preferred DropEffect", memo);
Clipboard.SetDataObject(data);

不幸的是,如果磁盘是 TrueCrypt 安装卷,则此方法不起作用。在 TrueCrypt 卷上执行此操作的方法是什么?

I use this code to copy files to Clipboard:

IDataObject data = new DataObject();
data.SetData(DataFormats.FileDrop, new string[] {@"X:\test.doc"});
MemoryStream memo = new MemoryStream(4);
byte[] bytes = new byte[] { (byte)(5), 0, 0, 0 };
memo.Write(bytes, 0, bytes.Length);
data.SetData("Preferred DropEffect", memo);
Clipboard.SetDataObject(data);

Unfortunately, this doesn't work if the disk is a TrueCrypt mounted volume. What is the way to do this on a TrueCrypt volume?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-10-22 17:18:46

不幸的是,我认为如果没有正确的 Shell ID 列表,您就无法摆脱困境,在我的 Windows 7 上,您的代码甚至无法与常规文件系统一起工作。正确的代码首先会提供 CIDL:

    var data = new DataObject();
    var files = new StringCollection() { @"T:\Test.doc" };
    data.SetFileDropList(files);
    data.SetData("Preferred DropEffect", true, new MemoryStream(new byte[] { 5, 0, 0, 0 }));
    data.SetData("Shell IDList Array", true, CreateShellIDList(files));
    Clipboard.SetDataObject(data, true);

其中 CreateShellIDList 创建所需 CIDA (CFSTR_SHELLIDLIST) 结构的二进制表示形式。实现如下:

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ILCreateFromPath(string path);
[DllImport("shell32.dll", CharSet = CharSet.None)]
public static extern void ILFree(IntPtr pidl);
[DllImport("shell32.dll", CharSet = CharSet.None)]
public static extern int ILGetSize(IntPtr pidl);

private static MemoryStream CreateShellIDList(StringCollection filenames)
{
    // first convert all files into pidls list
    int pos = 0;
    byte[][] pidls = new byte[filenames.Count][];
    foreach (var filename in filenames)
    {
        // Get pidl based on name
        IntPtr pidl = ILCreateFromPath(filename);
        int pidlSize = ILGetSize(pidl);
        // Copy over to our managed array
        pidls[pos] = new byte[pidlSize];
        Marshal.Copy(pidl, pidls[pos++], 0, pidlSize);
        ILFree(pidl);
    }

    // Determine where in CIDA we will start pumping PIDLs
    int pidlOffset = 4 * (filenames.Count + 2);
    // Start the CIDA stream stream
    var memStream = new MemoryStream();
    var sw = new BinaryWriter(memStream);
    // Initialize CIDA witha count of files
    sw.Write(filenames.Count);
    // Calcualte and write relative offsets of every pidl starting with root
    sw.Write(pidlOffset);
    pidlOffset += 4; // root is 4 bytes
    foreach(var pidl in pidls)
    {
        sw.Write(pidlOffset);
        pidlOffset += pidl.Length;
    }

    // Write the root pidl (0) followed by all pidls
    sw.Write(0);
    foreach(var pidl in pidls) sw.Write(pidl);
    // stream now contains the CIDA
    return memStream;
}

我不能把所有的功劳都归功于这里,我前段时间发现了这个 CIDA 代码,并将其移植到 c# 中。不太记得原始来源了,但到目前为止效果很好(我也刚刚在 TrueCrypt 上测试过)

Unfortunately, I do not think you can get away without a proper Shell ID list, on my Windows 7 your code doesn't even work with regular file system. The proper code would first and foremost provide a CIDL:

    var data = new DataObject();
    var files = new StringCollection() { @"T:\Test.doc" };
    data.SetFileDropList(files);
    data.SetData("Preferred DropEffect", true, new MemoryStream(new byte[] { 5, 0, 0, 0 }));
    data.SetData("Shell IDList Array", true, CreateShellIDList(files));
    Clipboard.SetDataObject(data, true);

Where CreateShellIDList creates a binary representation of CIDA (CFSTR_SHELLIDLIST) structure needed. The implementation is below:

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ILCreateFromPath(string path);
[DllImport("shell32.dll", CharSet = CharSet.None)]
public static extern void ILFree(IntPtr pidl);
[DllImport("shell32.dll", CharSet = CharSet.None)]
public static extern int ILGetSize(IntPtr pidl);

private static MemoryStream CreateShellIDList(StringCollection filenames)
{
    // first convert all files into pidls list
    int pos = 0;
    byte[][] pidls = new byte[filenames.Count][];
    foreach (var filename in filenames)
    {
        // Get pidl based on name
        IntPtr pidl = ILCreateFromPath(filename);
        int pidlSize = ILGetSize(pidl);
        // Copy over to our managed array
        pidls[pos] = new byte[pidlSize];
        Marshal.Copy(pidl, pidls[pos++], 0, pidlSize);
        ILFree(pidl);
    }

    // Determine where in CIDA we will start pumping PIDLs
    int pidlOffset = 4 * (filenames.Count + 2);
    // Start the CIDA stream stream
    var memStream = new MemoryStream();
    var sw = new BinaryWriter(memStream);
    // Initialize CIDA witha count of files
    sw.Write(filenames.Count);
    // Calcualte and write relative offsets of every pidl starting with root
    sw.Write(pidlOffset);
    pidlOffset += 4; // root is 4 bytes
    foreach(var pidl in pidls)
    {
        sw.Write(pidlOffset);
        pidlOffset += pidl.Length;
    }

    // Write the root pidl (0) followed by all pidls
    sw.Write(0);
    foreach(var pidl in pidls) sw.Write(pidl);
    // stream now contains the CIDA
    return memStream;
}

I can't take all the credit here, I found this CIDA code some time ago and just ported it to c#. Can't really remember the original source but it works well so far (I just tested it on TrueCrypt as well)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文