2013-09-16 47 views
1

有什么办法使文件和其他文件关联是指当任何一个试图复制任何这些文件,所有相关文件也复制。文件关联

像OS保护时,任何相关的文件被复制被复制隐藏文件。有没有办法在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; 
    } 
+2

你能告诉我们你想要完成的更多细节吗? – meilke

+0

除了运行一个可以查找指定目录中的文件的常量脚本,并且当它找不到它时,它会移动其他文件 - 我看不到其他方式。要么这个方法超出了我的知识。 –

+3

你指的是什么“操作系统保护隐藏文件”?我知道没有这样的行为。 –

回答

1

您可以使用FileSystemWatcher赶上被复制的文件,然后复制你想用它的任何文件。

+0

是,虽然FileSystemWatcher的不是没有它的问题:http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes – Arran

+0

什么如果我有多个目录中的多个关联文件。 –

+1

你也有这个问题,你的代码必须在任何时候运行这个工作,这是很难保证... –