Reading and writing registry returns error - c#

Well, I'm trying to write/read a SubKey on my application, but it give me a error.
Registry.LocalMachine.OpenSubKey("Software\\SAMP", true).SetValue("PlayerName", textBox1.Text);
string gamePath = Registry.LocalMachine.OpenSubKey("Software\\SAMP").GetValue("gta_sa_exe").ToString();
The error is on the first line:
"Object reference not set to an instance of an object."
Sorry, but I am pretty newbie on C# and just can't figure it out.

you try to get non existing key.
you should create the key before you try to get it.
here is a sample code
Registry.LocalMachine.CreateSubKey("Software\\SAMP");
Registry.LocalMachine.OpenSubKey("Software\\SAMP", true).SetValue("PlayerName", "tremp");
string gamePath = Registry.LocalMachine.OpenSubKey("Software\\SAMP").GetValue("PlayerName").ToString();

Related

Error "Object reference not set to an instance of an object." for CreateUserWithEmailAndPasswordAsync

I get error for code work before in Xamarin Android using Visual Studio 16.2.5:
I try to search new requirements for my may be outdated code
In main Activity
FirebaseApp.InitializeApp(Application.Context);
In function a call
var res = await FirebaseAuth.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
I get exception Object reference not set to an instance of an object.
I do downgrade to version 60.1142.1 to fast fix problem

H5T.copy: Failed to copy C_S1 with status -1

I am trying to create and fill an h5 file within the application I am working on. I need to use HDF5DotNet. When I try to create a string datatype, I get the following exception:
H5T.copy: Failed to copy C_S1 with status -1
when I call H5T.copy(H5T.H5Type.C-S1).
I have no idea what it means and how this exception can occur. Any help would be useful.
Thanks!

How to get ExpandEnvironmentVariables to return custom variables?

I have added a custom environment variable and I'm unable to get it to return in the ExpandEnvironmentVariables.
These 2 calls work fine:
string s = Environment.GetEnvironmentVariable("TEST", EnvironmentVariableTarget.Machine);
// s = "D:\Temp2"
string path = Environment.ExpandEnvironmentVariables(#"%windir%\Temp1");
// path = "C:\Windows\Temp1"
However, this call returns the same input string:
var path = Environment.ExpandEnvironmentVariables(#"%TEST%\Temp1");
// path = "%TEST%\\Temp1"
I expect to get D:\Temp2\Temp1
What am I missing to correctly get the custom EnvironmentVariable in this last call?
Hans and Evk were correct in their comments. Since no one wanted to add an answer I'll close this question out.
For whatever reason ExpandEnvironmentVariables will not get any keys which were added after an application started. I also tested this with a running Windows Service. It was only after I restarted the service that new keys were found and populated.
This behavior is not documented in the Microsoft Documentation.

Error while reading mail from pop3client

I am trying to read emails from pop3clent with following code:
Pop3Client pop = new Pop3Client();
pop.Connect(mailHost, portNum, false);
pop.Authenticate(userName, passWord,AuthenticationMethod.UsernameAndPassword);
And I am getting error while authenticating:
"Object reference not set to an instance of an object."
You might be getting the error elsewhere if you are sure all your objects in the code you posted does not contain null values.

Windows Service Webbrowser object invalid cast exception error

I'm having a bit of trouble with a Windows Service webbrowser object. It's attempting to load in values of username and password to a site but keeps failing and throwing the following error:
System.InvalidCastException: Specified cast is not valid.
at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
at System.Windows.Forms.WebBrowser.get_Document()
at MyWindowsService.MyDataProcessor.login()
The code that I'm using to make this call is:
MyWebBrowser.Document.All["Login"].SetAttribute("Value", username);
MyWebBrowser.Document.All["Password"].SetAttribute("Value", password);
MyWebBrowser.Document.All["submit"].InvokeMember("Click");
Any ideas as to why it keeps failing? Thanks in advance for the help.
I'm not sure if this solves the problem, but you can check InvokeRequired property on the current object, or WebBrowser.InvokeRequired, and use something like MethodInvoker to call your function or a helper function to access WebBrowser.Document.
http://www.megasolutions.net/cSharp/(WebBrowser_Document-==-null)-throws-InvalidCastException-43126.aspx
I had a similar problem using SHDocVW.WebBrowserClass. I got an InvalidCastException when I tried to access Document.all from an instance of SHDocVW.WebBrowserClass (from the main thread) and I was able to fix it by casting to IHTMLDocument2 instead of HTMLDocument. This took me a long time to figure out because casting to HTMLDocument works most of the time.
SHDocVW.WebBrowserClass Explorer = [instance of IE];
((IHTMLDocument2)Explorer.Document).all // works all the time
((HTMLDocument)Explorer.Document).all // works some times
I hope this helps someone.

Categories

Resources