2

冷知識 - Window 資料夾「唯讀」屬性怎麼用?

 1 month ago
source link: https://blog.darkthread.net/blog/win-dir-readonly-attrib/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Window 資料夾「唯讀」屬性怎麼用?-黑暗執行緒

同事問起 Windows 資料夾的唯讀屬性。經驗裡我只改過檔案的唯讀屬性解決無法覆寫問題,從沒想過去管資料夾唯讀與否,但檔案總管看資料夾確實有個唯讀設定,它是做什麼用的?有什麼效果?

經過實測,使用 attr +r 指令,設定唯讀後,資料夾照樣可以新增、覆寫及刪除檔案:

Fig1_638461624731289737.png

不過我注意到,檔案總管上的唯讀顯示的是 - 而不是打勾,這通常代表部分選取。而唯讀旁有註記 - 僅套用到資料夾中的檔案 (意思是不包含資料夾本身)。若改成勾再套用,會出現提示並只能選取「將變更套用到這個資料夾、子資料夾及檔案」選項。

Fig2_638461631085527335.png

回到資料夾本身的唯讀設定,在本草綱目對 FILE_ATTRIBUTE_READONLY記載可找到答案:

A file that is read-only. Applications can read the file, but cannot write to it or delete it. This attribute is not honored on directories.
檔案設成唯讀時,程式可讀取但不能覆寫或刪除;而此屬性對目錄無效。

You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7 這篇 KB 則有更詳細說明:

Note unlike the Read-only attribute for a file, the Read-only attribute for a folder is typically ignored by Windows, Windows components and accessories, and other programs. For example, you can delete, rename, and change a folder with the Read-only attribute by using Windows Explorer.

The Read-only and System attributes is only used by Windows Explorer to determine whether the folder is a special folder, such as a system folder that has its view customized by Windows (for example, My Documents, Favorites, Fonts, Downloaded Program Files), or a folder that you customized by using the Customize tab of the folder's Properties dialog box. As a result, Windows Explorer does not allow you to view or change the Read-only or System attributes of folders. When a folder has the Read-Only attribute set it causes Explorer to request the Desktop.ini of that folder to see if any special folder settings need to be set. It has been seen where if a network share that has a large amount of folders set to Read-only, it can cause Explorer to take longer then what is expected to render the contents of that share while it waits on the retrieval of the Desktop.ini files. The slower the network connectivity to the share the longer this process can take to the point where Explorer may timeout waiting for the data and render nothing or appear to hang.

Note In some previous versions of Windows, you can change the Read-only attribute for folders by using the Properties dialog box for the folder, but no versions of Windows permit you to change the System attribute by using Windows Explorer.

與檔案的唯讀屬性不同,資料夾的唯讀屬性通常在 Windows、Windows 元件和附件以及其他程式中都會被忽略。例如,我們可以用 Windows Explorer 刪除、重新命名和更改具有唯讀屬性的資料夾。

唯讀和系統屬性只被 Windows Explorer 用來判斷資料夾是否為特殊資料夾,例如 可自訂其檢視的系統資料夾(例如「我的文件」、「最愛」、「字型」、「下載的程式檔案」)或者使用者自訂檢視的資料夾。在 Windows 2003/XP/Vista/7 時代,甚至不允許查看或更改資料夾的唯讀或系統屬性。資料夾設定為唯讀屬性時還可能影響效能,甚至導致 Windows Explorer 超時等待資料無法顯示內容或卡住。KB 提到 在一些較早版本的 Windows 中可以使用資料夾的「屬性」對話方塊來更改唯讀屬性。

因此到了 Windows 10/11,檔案總管又可以檢視及設定資料夾唯讀,但主要被用來設定檔案及子資料唯讀與否,資料夾本身的唯讀屬性無關痛癢,結案。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK