防止文件在运行时被 (Deleted/Edited) VB.NET
Prevent file from being (Deleted/Edited) in Runtime VB.NET
是否可以保护项目目录位置中的文件。
例如(test.txt 或 test.bat 或...等)。
我有重要的数据不想在运行项目时在 txt 中更改。
累了很多东西。
- 通过权限/文件流锁定文件(但效果不佳,通过更改用户权限很容易解锁。
now my code i tried but it lock only the project not the file, in runtime
let's say my file type (test.txt or test.bat or ... Etc).
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Security.AccessControl
Imports System.Security.Principal
Public Class c_AntiKill
<DllImport("advapi32.dll", SetLastError:=True)> _
Shared Function GetKernelObjectSecurity(Handle As IntPtr, securityInformation As Integer, <Out> pSecurityDescriptor As Byte(), nLength As UInteger, ByRef lpnLengthNeeded As UInteger) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Shared Function SetKernelObjectSecurity(Handle As IntPtr, securityInformation As Integer, <[In]> pSecurityDescriptor As Byte()) As Boolean
End Function
<DllImport("kernel32.dll")> _
Shared Function GetCurrentProcess() As IntPtr
End Function
Protected Function GetProcessSecurityDescriptor(processHandle As IntPtr) As RawSecurityDescriptor
Dim psd() As Byte = New Byte(1) {}
Dim bufSizeNeeded As UInteger
GetKernelObjectSecurity(processHandle, &H4, psd, 0, bufSizeNeeded)
psd = New Byte(bufSizeNeeded) {}
If bufSizeNeeded < 0 OrElse bufSizeNeeded > Short.MaxValue Then
Throw New Win32Exception()
End If
If Not GetKernelObjectSecurity(processHandle, &H4, psd, bufSizeNeeded, bufSizeNeeded) Then
Throw New Win32Exception()
End If
Return New RawSecurityDescriptor(psd, 0)
End Function
Protected Sub SetProcessSecurityDescriptor(processHandle As IntPtr, dacl As RawSecurityDescriptor)
Dim rawsd As Byte() = New Byte(dacl.BinaryLength - 1) {}
dacl.GetBinaryForm(rawsd, 0)
If Not SetKernelObjectSecurity(processHandle, &H4, rawsd) Then
Throw New Win32Exception()
End If
End Sub
Public Sub c_ImAntiKill()
Dim hProcess As IntPtr = GetCurrentProcess()
Dim dacl = GetProcessSecurityDescriptor(hProcess)
dacl.DiscretionaryAcl.InsertAce(0, New CommonAce(AceFlags.None, AceQualifier.AccessDenied, CInt(&HF0000 Or &H100000 Or &HFFF), New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing), False, Nothing))
SetProcessSecurityDescriptor(hProcess, dacl)
' Console.ReadKey()
End Sub
End Class
您可以使用用户的许可。
从另一个线程回答。(工作正常)
是否可以保护项目目录位置中的文件。
例如(test.txt 或 test.bat 或...等)。
我有重要的数据不想在运行项目时在 txt 中更改。
累了很多东西。
- 通过权限/文件流锁定文件(但效果不佳,通过更改用户权限很容易解锁。
now my code i tried but it lock only the project not the file, in runtime
let's say my file type (test.txt or test.bat or ... Etc).
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Security.AccessControl
Imports System.Security.Principal
Public Class c_AntiKill
<DllImport("advapi32.dll", SetLastError:=True)> _
Shared Function GetKernelObjectSecurity(Handle As IntPtr, securityInformation As Integer, <Out> pSecurityDescriptor As Byte(), nLength As UInteger, ByRef lpnLengthNeeded As UInteger) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Shared Function SetKernelObjectSecurity(Handle As IntPtr, securityInformation As Integer, <[In]> pSecurityDescriptor As Byte()) As Boolean
End Function
<DllImport("kernel32.dll")> _
Shared Function GetCurrentProcess() As IntPtr
End Function
Protected Function GetProcessSecurityDescriptor(processHandle As IntPtr) As RawSecurityDescriptor
Dim psd() As Byte = New Byte(1) {}
Dim bufSizeNeeded As UInteger
GetKernelObjectSecurity(processHandle, &H4, psd, 0, bufSizeNeeded)
psd = New Byte(bufSizeNeeded) {}
If bufSizeNeeded < 0 OrElse bufSizeNeeded > Short.MaxValue Then
Throw New Win32Exception()
End If
If Not GetKernelObjectSecurity(processHandle, &H4, psd, bufSizeNeeded, bufSizeNeeded) Then
Throw New Win32Exception()
End If
Return New RawSecurityDescriptor(psd, 0)
End Function
Protected Sub SetProcessSecurityDescriptor(processHandle As IntPtr, dacl As RawSecurityDescriptor)
Dim rawsd As Byte() = New Byte(dacl.BinaryLength - 1) {}
dacl.GetBinaryForm(rawsd, 0)
If Not SetKernelObjectSecurity(processHandle, &H4, rawsd) Then
Throw New Win32Exception()
End If
End Sub
Public Sub c_ImAntiKill()
Dim hProcess As IntPtr = GetCurrentProcess()
Dim dacl = GetProcessSecurityDescriptor(hProcess)
dacl.DiscretionaryAcl.InsertAce(0, New CommonAce(AceFlags.None, AceQualifier.AccessDenied, CInt(&HF0000 Or &H100000 Or &HFFF), New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing), False, Nothing))
SetProcessSecurityDescriptor(hProcess, dacl)
' Console.ReadKey()
End Sub
End Class
您可以使用用户的许可。
从另一个线程回答。(工作正常)