有没有办法将文件与任何其他文件相关联意味着当任何人尝试复制任何这些文件时,所有关联的文件也会被复制。
就像复制任何关联文件时复制的受操作系统保护的隐藏文件一样。有没有办法在 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;
}