如何保护文件

发布于 2024-11-06 05:40:48 字数 1158 浏览 0 评论 0原文

我正在使用 bat 文件来保护某些文件。它要求用户输入密码才能使该文件夹可见。

但在我的 C# 程序中,我想打开该文件,但当它已被 bat 文件隐藏时,它似乎找不到该文件 (if (File.Exists(ls_Path)) 返回 false)。

它与我的 bat 文件有关吗?

我在窗口的正常隐藏中测试了我的程序仍然能够从路径中读出。

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 FAIL
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

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 FAIL
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-11-13 05:40:48

使用 VBScript,无需检查文件是否存在,只需尝试打开它并使用错误陷阱即可。我对批处理不太熟悉,但我怀疑您可以使用类似的技术。

而不是:

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

你会:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文