簡體   English   中英

應用文件夾圖標更改

[英]Apply Folder Icon Change

我正在嘗試更改文件夾的圖標。 下面的代碼完成了我在網上找到的所有內容,但圖標永遠不會改變。 我可能沒有“應用”更改嗎?

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);

在制作這樣的文件時,最好先使用資源管理器或記事本,然后編寫/調整您的代碼以匹配生成的任何內容。 否則,很難確定問題是出在文件還是代碼上。

我相信使這項工作的最低要求是Desktop.ini必須標記為System並且父目錄必須標記為ReadOnlySystem也可以在那里工作,但我知道ReadOnly肯定可以)。 因此,您的代碼使用了正確的屬性,但仍然存在一些問題。

您的if ... else ...塊是說“如果此路徑中存在文件,則在該路徑上創建一個目錄,然后刪除該路徑上的文件,然后在該路徑上創建一個文件。” 當然,目錄不應該也不能與文件具有相同的路徑。 我假設您正在刪除並重新創建文件以在它已經存在時清除內容,但是File.Create()覆蓋(截斷)現有文件,從而使對File.Delete()File.Exists()的調用File.Exists()不必要。

更重要的是這條線...

di.Attributes &= ~FileAttributes.ReadOnly;

...有兩個問題。 首先,您將目錄的屬性與ReadOnly的否定進行ReadOnly ,這具有刪除ReadOnly並保持其他屬性相同的效果。 您希望確保在目錄上設置ReadOnly ,因此您希望執行與您使用的代碼相反的操作:OR 目錄的屬性為ReadOnly (未否定)...

di.Attributes |= FileAttributes.ReadOnly;

此外,無論您是否創建了目錄,都需要該屬性集,因此該行應該移到if ... else ...

另一個問題是對File.SetAttributes()的連續調用。 在這三個調用之后,文件的屬性將僅為Hidden ,因為這是最后一次調用的值。 相反,您需要在單個調用中組合(按位或)這些屬性。

其他一些小調整......

  • 如您所知,因為您正在對其調用Dispose() ,所以File.Create()返回一個FileStream到該文件。 與其扔掉它,您還可以使用它來創建您的StreamWriter ,無論如何,它必須在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*()方法在輸入文件為HiddenReadOnly失敗。 我們可以使用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;

我從使用File更改為FileInfo因為這使這更容易一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM