[Visual Basic] - Problem z tworzeniem wartości w rejestrze


(blondkarol) #1

Witam,

Piszę program, który pozwala blokować niektóre funkcje Windowsa, np. Regedit, CMD itp.

Oto fragment kodu:

If CheckBox3.Checked = True Then
            MessageBox.Show("Zablokowano CMD")
            sciezka(1) = "Software\Policies\Microsoft\Windows\System"
            klucz(1) = Registry.CurrentUser.OpenSubKey(sciezka(1), True)
            klucz(1).SetValue("DisableCMD", 1)

        ElseIf CheckBox3.Checked = False Then

            sciezka(1) = "Software\Policies\Microsoft\Windows\System"
            klucz(1) = Registry.CurrentUser.OpenSubKey(sciezka(1), True)
            klucz(1).SetValue("DisableCMD", 0)
        End If

Gdy uruchomię program wywala błąd:

System.Security.SecurityException was unhandled
  HResult=-2146233078
  Message=Żądany dostęp do rejestru jest niedozwolony.
  Source=mscorlib
  StackTrace:
       w System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
       w Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
       w Windows7RegistryEditor.Form1.Timer1_Tick(Object sender, EventArgs e) w C:\Users\Gosia\documents\visual studio 2010\Projects\Windows7RegistryEditor\Windows7RegistryEditor\Form1.vb:wiersz 66
       w System.Windows.Forms.Timer.OnTick(EventArgs e)
       w System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.RunDialog(Form form)
       w System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       w System.Windows.Forms.Form.ShowDialog()
       w Windows7RegistryEditor.LoginForm1.OK_Click(Object sender, EventArgs e) w C:\Users\Gosia\documents\visual studio 2010\Projects\Windows7RegistryEditor\Windows7RegistryEditor\LoginForm1.vb:wiersz 34
       w System.Windows.Forms.Control.OnClick(EventArgs e)
       w System.Windows.Forms.Button.OnClick(EventArgs e)
       w System.Windows.Forms.Button.PerformClick()
       w System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
       w System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
       w System.Windows.Forms.Control.PreProcessMessage(Message& msg)
       w System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
       w System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
       w System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
       w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.Run(ApplicationContext context)
       w Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       w Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       w Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       w Windows7RegistryEditor.My.MyApplication.Main(String[] Args) w 17d14f5c-a337-4978-8281-53493378c1071.vb:wiersz 81
       w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       w System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Wydaję mi się, że chodzi o brak uprawnień.

 

Czy wie ktoś może jak nadać odpowiednie uprawnienia?

 

Pozdrawiam,

blondkarol


(Edgarus) #2

Drugi temat?

 

Tam napisałem co zrobić http://forum.dobreprogramy.pl/visual-basic-problem-z-tworzeniem-klucza-w-rejestrze-t482054#entry3082357/