How to remove a single Attribute (e.g. ReadOnly) from a File?

前端 未结 7 1733
南方客
南方客 2020-11-28 08:54

Let say, a file has the following attributes: ReadOnly, Hidden, Archived, System. How can I remove only one Attribute? (for example ReadOnly)

7条回答
  •  一生所求
    2020-11-28 09:38

    For a one line solution (provided that the current user has access to change the attributes of the mentioned file) here is how I would do it:

    VB.Net

    Shell("attrib file.txt -r")
    

    the negative sign means to remove and the r is for read-only. if you want to remove other attributes as well you would do:

    Shell("attrib file.txt -r -s -h -a")
    

    That will remove the Read-Only, System-File, Hidden and Archive attributes.

    if you want to give back these attributes, here is how:

    Shell("attrib file.txt +r +s +h +a")
    

    the order does not matter.

    C#

    Process.Start("cmd.exe", "attrib file.txt +r +s +h +a");
    

    References

    • Google
    • ComputerHope.com - Lots of examples and OS specific notes
    • Microsoft - Page is for XP, but probably applies to later versions
    • Wikipedia - Particularly the Particularities section

提交回复
热议问题