首页 > 解决方案 > 应用文件夹图标更改

问题描述

我正在尝试更改文件夹的图标。下面的代码完成了我在网上找到的所有内容,但图标永远不会改变。我可能没有“应用”更改吗?

string createdFile = Path.Combine(@"C:\Users\np\Desktop\PUTEST", "desktop.ini");
if (File.Exists(createdFile))
{
    var di = new DirectoryInfo(createdFile);
    di.Attributes &= ~FileAttributes.ReadOnly;

    File.Delete(createdFile);
    File.Create(createdFile).Dispose();
}
else
{
    File.Create(createdFile).Dispose();
}


//string iconPath = @"%SystemRoot%\system32\SHELL32.dll";
string iconPath = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\SHELL32.dll");
string iconIndex = "-183";
using (TextWriter tw = new StreamWriter(createdFile))
{
    tw.WriteLine("[.ShellClassInfo]");
    tw.WriteLine("IconResource=" + iconPath + "," + iconIndex);
    //tw.WriteLine("IconFile=" + iconPath);
    //tw.WriteLine("IconIndex=" + iconIndex);

}


File.SetAttributes(createdFile, System.IO.FileAttributes.ReadOnly);
File.SetAttributes(createdFile, System.IO.FileAttributes.System);
File.SetAttributes(createdFile, System.IO.FileAttributes.Hidden);

标签: c#windowsiconsfile-attributesdesktop.ini

解决方案


在制作这样的文件时,最好先使用资源管理器或记事本,然后编写/调整代码以匹配生成的任何内容。否则,很难确定问题出在您的文件还是代码上。

我相信完成这项工作的最低要求是Desktop.ini必须标记System并且必须标记父目录ReadOnlySystem可能也可以在那里工作,但我知道ReadOnly肯定可以)。因此,您的代码使用正确的属性,但仍然存在一些问题。

您的if ... else ...块说“如果此路径存在文件,则在该路径创建一个目录,然后删除该路径的文件,然后在该路径创建一个文件。” 当然,目录不应该也不能与文件具有相同的路径。我假设您正在删除并重新创建文件以清除已经存在的内容,但是会File.Create()覆盖(截断)现有文件,从而对两者进行调用File.Delete()并且是File.Exists()不必要的。

更重要的是这条线...

di.Attributes &= ~FileAttributes.ReadOnly;

...有两个问题。首先,您将目录的属性与 的否定进行“与”运算ReadOnly,这具有删除ReadOnly和保持其他属性相同的效果。您要确保在目录ReadOnly设置,因此您想要执行与您使用的代码相反的操作:或目录的属性与ReadOnly(不否定)...

di.Attributes |= FileAttributes.ReadOnly;

此外,无论您是否创建目录,都需要设置该属性,因此该行应移到if ... else ....

另一个问题是对File.SetAttributes(). 在这三个调用之后,文件的属性将是 only Hidden,因为那是最后一次调用的值。相反,您需要在一次调用中组合(按位或)这些属性。

其他几个小调整...

  • 如您所知,因为您正在调用Dispose()它,所以File.Create()将 a 返回FileStream到该文件。您可以使用它来创建您的 ,而不是扔掉它StreamWriter,无论如何,它必须在幕后创建一个。更好的是,File.CreateText()改为打电话,它会StreamWriter为你创建。
  • 文件中支持环境变量Desktop.ini,因此您不必自己扩展它们。%SystemRoot%如果您将文件从一个系统复制到另一个系统,或者该目录位于由具有不同值的多个系统访问的网络共享上,这将使文件在系统之间可移植。

结合上述所有更改,您的代码将变为...

// Create a new directory, or get the existing one if it exists
DirectoryInfo directory = Directory.CreateDirectory(@"C:\Users\np\Desktop\PUTEST");
directory.Attributes |= FileAttributes.ReadOnly;

string filePath = Path.Combine(directory.FullName, "desktop.ini");
string iconPath = @"%SystemRoot%\system32\SHELL32.dll";
string iconIndex = "-183";

using (TextWriter tw = File.CreateText(filePath))
{
    tw.WriteLine("[.ShellClassInfo]");
    tw.WriteLine("IconResource=" + iconPath + "," + iconIndex);
    //tw.WriteLine("IconFile=" + iconPath);
    //tw.WriteLine("IconIndex=" + iconIndex);
}

File.SetAttributes(filePath, FileAttributes.ReadOnly | FileAttributes.System | FileAttributes.Hidden);

一个问题是,如果您连续运行两次上述代码,则会引发异常。这是因为File.Create*()如果输入文件是Hidden或,则方法会失败ReadOnly。我们可以使用new FileStream()作为替代方案,但如果文件是ReadOnly. 相反,我们只需要在打开之前从任何现有输入文件中删除这些属性...

// Create a new directory, or get the existing one if it exists
DirectoryInfo directory = Directory.CreateDirectory(@"C:\Users\np\Desktop\PUTEST");
directory.Attributes |= FileAttributes.ReadOnly;

string filePath = Path.Combine(directory.FullName, "desktop.ini");
FileInfo file = new FileInfo(filePath);

try
{
    // Remove the Hidden and ReadOnly attributes so file.Create*() will succeed
    file.Attributes = FileAttributes.Normal;
}
catch (FileNotFoundException)
{
    // The file does not yet exist; no extra handling needed
}

string iconPath = @"%SystemRoot%\system32\SHELL32.dll";
string iconIndex = "-183";

using (TextWriter tw = file.CreateText())
{
    tw.WriteLine("[.ShellClassInfo]");
    tw.WriteLine("IconResource=" + iconPath + "," + iconIndex);
    //tw.WriteLine("IconFile=" + iconPath);
    //tw.WriteLine("IconIndex=" + iconIndex);
}

file.Attributes = FileAttributes.ReadOnly | FileAttributes.System | FileAttributes.Hidden;

我从 using 更改为FileFileInfo因为这使这更容易一些。


推荐阅读