1

Is there any way to associate a file with any other file means when any one try to copy any of those file, all associated files also copied.

Like OS protected hidden files that are copied when any of associated file is copied. Is there any way to do this in c# or other work around ?

Ok i'm going to narrow down by problem. What i'm going to achieve custom icon overlay for specific kind of files. I have implemented this by using a hidden file in a directory that contains name of files whose icons can be overlay. but my problem is that when any of file whose path is writen in that hidden file is moved, the overlay icons are set to default. May be code snippets given below will help to clear my question.

[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;
    }
Marc
  • 3,905
  • 4
  • 21
  • 37
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36

1 Answers1

1

You can use FileSystemWatcher to catch a file being copied , and then copy any file you want with it.

Omri Btian
  • 6,499
  • 4
  • 39
  • 65