查看單個文章
舊 2016-10-05, 04:15 PM   #1
mini
管理版主
 
mini 的頭像
榮譽勳章
UID - 4144
在線等級: 級別:96 | 在線時長:9659小時 | 升級還需:138小時級別:96 | 在線時長:9659小時 | 升級還需:138小時級別:96 | 在線時長:9659小時 | 升級還需:138小時級別:96 | 在線時長:9659小時 | 升級還需:138小時級別:96 | 在線時長:9659小時 | 升級還需:138小時級別:96 | 在線時長:9659小時 | 升級還需:138小時
註冊日期: 2002-12-07
文章: 13246
精華: 0
現金: 26235 金幣
資產: 3024045 金幣
預設 [VB.Net] 以命令列指令方式修改檔案屬性(針對指定資料夾變成唯讀)

.使用整合開發軟體 Microsoft Visual Studio 2010
.起始頁 -> 新增專案 -> 主控台應用程式
.按Ctrl+A 反白選取貼上以下內容 代換之

PHP 語法:
Option Explicit On
Imports System
.IO

Module MainModule

    Sub Main
(ByVal cmdArgs() As String)
        If 
cmdArgs.Length 0 Then Exit Sub
        
If Right(cmdArgs(0), 1) <> "\" Then cmdArgs(0) = cmdArgs(0) & "\"

        Dim oFile As Array = IO.Directory.GetFileSystemEntries(cmdArgs(0), "
*.*", SearchOption.TopDirectoryOnly)

        Dim infoReader As System.IO.FileInfo
        Dim attributeReader As System.IO.FileAttributes

        For Each sFile As String In oFile
            infoReader = My.Computer.FileSystem.GetFileInfo(sFile)
            attributeReader = infoReader.Attributes

            If Not ((attributeReader And 1) = FileAttributes.ReadOnly) Then
                infoReader.Attributes = FileAttributes.ReadOnly Or attributeReader
            End If
        Next

    End Sub

End Module 
技術重點:
.使用引數的方式指定 資料夾
cmdArgs(0) 就是傳入的字串

.使用方法 IO.Directory.GetFileSystemEntries 獲取指定資料夾下的所有檔案 (存放在 Array類變數中)
.使用System.IO.FileInfo類變數得到檔案資訊集
.取.Attributes 得到目前檔案擁有的屬性 (放入attributeReader)

.只針對非唯讀檔案作更改使用
Not ((attributeReader And 1) = FileAttributes.ReadOnly) 作條件
FileAttributes.ReadOnly的常數值是1, 目前檔案擁有的屬性 與1 作And布林運算
如果還是等於 1 就表示目前擁有唯讀屬性
如果Not則進入條件修改之
|
V
.現在要加上唯讀屬性又保留其他屬性
就用 FileAttributes.ReadOnly Or attributeReader
並回存到 .Attributes 以更改檔案屬性
=============

最後建置成 改檔案屬性.exe 命令列用執行檔

可以用批次檔方式包裝再用定時執行方式執行
這個方法可以有效杜絕勒索病毒對你的重要檔案作修改
~~~批次檔.bat~~~
改檔案屬性.exe d:\我的重要檔案\
~~~~~~~~~~~~

雖然 Windows 檔案總管 也可輕易做到
但要說 定時/點擊 執行的話
使用 寫好的.bat的方式最方便

此帖於 2016-10-08 04:30 PM 被 mini 編輯.
mini 目前離線  
送花文章: 1999, 收花文章: 7956 篇, 收花: 26748 次
回覆時引用此帖