开发者

how to protect files

开发者 https://www.devze.com 2023-03-05 05:36 出处:网络
I am using a bat file to protect some files. It requires the user to key in a password to make the folder visible.

I am using a bat file to protect some files. It requires the user to key in a password to make the folder visible.

But in my C# program I want to open the file but it seems it can't find the file when it is already hidden by the bat file (if (File.Exists(ls_Path)) returns false).

Is it anything related to my bat file?

I tested in normal hide at window my program still able to read out from the path.

cls
@ECHO OFF
title Folder Locker
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Locker goto MDLOCKER
:CONFIRM
echo Are you sure u want to Lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%==123 goto FAI开发者_如何学PythonL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Locker
echo Locker created successfully
goto End
:End


With VBScript, rather than check to see if the file exists, just try to open it and use an error trap. I'm not as familiar with batch, but I suspect you can use a similar technique.

Instead of:

    If objFSO.FileExists(someFileVariable) Then _
Set objTESTfile = objFSO.OpenTextFile(someFileVariable, ForReading, True, TristateFalse)

You would have:

On Error Resume Next
Set objTESTfile = objFSO.OpenTextFile(someFileVariable, ForReading, True, TristateFalse)
If Err.Number= 0 Then
    ' opened ok, do stuff
Else
    ' failed, do something else
End If
0

精彩评论

暂无评论...
验证码 换一张
取 消