文件关联

时间:2013-09-16 13:31:52

标签: c# file-io

有没有办法将文件与任何其他文件相关联意味着当任何人尝试复制任何这些文件时,所有相关文件也会被复制。

与受保护的受保护的隐藏文件类似,在复制任何关联文件时复制。有没有办法在c#或其他工作中做到这一点?

好的,我要按问题缩小范围。我要为特定类型的文件实现自定义图标叠加。我已经通过在目录中使用隐藏文件来实现这一点,该目录包含图标可以叠加的文件名。但我的问题是,当移动该隐藏文件中写入路径的任何文件时,叠加图标将设置为默认值。可能是下面给出的代码片段有助于澄清我的问题。

[ComVisible(false)]
[Guid("1fd5bae8-257a-461a-91ea-869a810e0ccc")]
public class MyIconOverlayHandlersBase : IShellIconOverlayIdentifier
{
    string fileName = string.Empty;

    #region Class Properties

    protected virtual string OverlayIconFilePath
    {
        get
        {
            return string.Empty;
        }
    }

    protected virtual string TargetDirectory
    {
        get
        {
            return string.Empty;
        }
    }

    protected virtual int Priority
    {
        get
        {
            return 0;  // 0-100 (0 is highest priority)
        }
    }

    protected virtual string FileNameStart
    {
        get
        {
            return fileName;
        }

        set
        {
            fileName = value;
        }
    }

    #endregion Class Properties

    #region IShellIconOverlayIdentifier Members

    public int IsMemberOf(string path, uint attributes)
    {
        List<string> filesName = IsHiddenVDFile(path);

        if (filesName.Count <= 0)
        {
            return (int)HRESULT.S_FALSE;
        }

        try
        {
            string f = Path.GetFileName(path);
            string newFile = filesName.FirstOrDefault(t => t.Equals(f));
            if (!String.IsNullOrEmpty(newFile))
            {
                unchecked
                {
                    return
                        Path.GetFileName(path).StartsWith(newFile, StringComparison.InvariantCultureIgnoreCase) ?
                        (int)HRESULT.S_OK :
                        (int)HRESULT.S_FALSE;
                }
            }
            else
            {
                return (int)HRESULT.S_FALSE;
            }
        }
        catch
        {
            unchecked
            {
                return (int)HRESULT.E_FAIL;
            }
        }
    }

    private List<string> GetFilesName(string info)
    {
        List<string> files = new List<string>();
        StreamReader reader = new StreamReader(info);
        string fileNames = reader.ReadToEnd();
        string[] f = fileNames.Split('\r');
        foreach (string file in f)
        {
            files.Add(file.Trim());
        }
        reader.Close();
        return files;
    }

    private List<string> IsHiddenVDFile(string path)
    {
        DirectoryInfo info = new DirectoryInfo(path).Parent;
        if (info == null)
        {
            return new List<string>();
        }
        FileInfo[] files = info.GetFiles();

        var filtered = files.Select(f => f).Where(f => (f.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);

        foreach (var f in filtered)
        {
            if (f.Name.Equals("01AVDNames.txt"))
            {
                return GetFilesName(f.FullName);
            }
        }
        return new List<string>();
    }

    public int GetOverlayInfo(IntPtr iconFileBuffer, int iconFileBufferSize, out int iconIndex, out uint flags)
    {
        string fname = OverlayIconFilePath;
        int bytesCount = System.Text.Encoding.Unicode.GetByteCount(fname);
        byte[] bytes = System.Text.Encoding.Unicode.GetBytes(fname);
        if (bytes.Length + 2 < iconFileBufferSize)
        {
            for (int i = 0; i < bytes.Length; i++)
            {
                Marshal.WriteByte(iconFileBuffer, i, bytes[i]);
            }
            //write the "\0\0"
            Marshal.WriteByte(iconFileBuffer, bytes.Length, 0);
            Marshal.WriteByte(iconFileBuffer, bytes.Length + 1, 0);
        }

        iconIndex = 0;
        flags = (int)(HFLAGS.ISIOI_ICONFILE | HFLAGS.ISIOI_ICONINDEX);
        return (int)HRESULT.S_OK;
    }

    public int GetPriority(out int priority)
    {
        priority = Priority;
        return (int)HRESULT.S_OK;
    }

1 个答案:

答案 0 :(得分:1)

您可以使用FileSystemWatcher来捕获正在复制的文件,然后使用它复制所需的任何文件。