如何解决表格申请注册表项
我有
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using Microsoft.Win32;
namespace Just_play_sounds_2
{
public partial class Form1 : Form
{
private SoundPlayer soundPlayer;
private RegistryKey reg;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
}
private void Form1_Load(object sender,EventArgs e)
{
this.Hide();
reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurentVersion\\Run",true);
reg.SetValue("Play sounds",Application.ExecutablePath.ToString());
}
}
}
并且正在标记这行代码
reg.SetValue("Play sounds",Application.ExecutablePath.ToString());
我已经尽一切努力使它正常工作,但它根本无法工作。我搜索了所有可以找到的东西,为此我真的可以使用一些帮助。如果您回答此问题,可以告诉我我的注册表代码做错了什么。
错误代码
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Just play sounds 2
StackTrace:
at Just_play_sounds_2.Form1.Form1_Load(Object sender,EventArgs e) in D:\Projects\Just play sounds 2\Just play sounds 2\Form1.cs:line 37
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,Int32 wParam,Int32 lParam)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Just_play_sounds_2.Program.Main() in D:\Projects\Just play sounds 2\Just play sounds 2\Program.cs:line 19
解决方法
通过您的编辑,现在我可以看到您的问题是What is a NullReferenceException,and how do I fix it?的重复项。最可能是因为reg
变量是null
,这又很可能是由于我已经提到过的印刷错误。 –彼得·杜尼奥(Peter Duniho)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。