C_Work Summary

Posted by Mark W on Thu, 03 Oct 2019 00:12:20 +0200

Passing Values Between Winform Forms

1. By constructor

The implementation code is as follows:
Form Form1

		LoginData ld = null;
		public LoginData Ld
        {
            get
            {
                return ld;
            }

            set
            {
                ld = value;
            }
        }

Called in Form2 as follows

LoginData ld = null;
Login form = new Login();
ld = form.Ld;

2. Through static variables

First, define a static variable in Form1 and assign it to it

public static string ErrorStr = "";
ErrorStr ="text";

Called in Form2 as follows

string err = Form1.ErrorStr;

3. Passing values when calling another form

Calling Form Form2 in Form 1

string value = "text";
Form2 form = new Form2(value);
form.ShowDialog();

Receive values in Form2

string value = "";
public FaultHandling(string value )
{
 	this.value = value ;
}

Winform window exit

1.this.Close() closes the current form
2.Application.Exit() Application backs out and closes all forms

Operational Registry

Registry Key: using Microsoft.Win32;
Read the contents of registry keys

//public static object GetValue (string keyName, string valueName, object defaultValue);
//keyName: Registry full path valueName: key name defaultValue: valueName does not have a value to return

//Example:
string RegeditPath="HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer";
string Key="svcUpdateVersion"
string registData = (string)Registry.GetValue(RegeditPath,Key, "");

Registry.LocalMachine.OpenSubKey() can also be used to read

In the process of running, it is prompted that there is no permission to operate the registry. The solution is as follows:
Add an Application manifest file (application manifest file) to the program and replace asInvoker with requireAdministrator. Generally, after adding this file, when debugging the program, the program will ask to start vs as administrator.

Read JSON files

1. Call the interface to get the json file content

//Service Address is the json file address
string serviceAddress = ServiceAddress;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;

//Processing of read content
JObject job = JObject.Parse(retString);
var value = job [key].ToString();

2. Read the content of the local json file

StreamReader sr = new StreamReader(ServiceAddress);
string retString = sr.ReadToEnd();
sr.Close();

//Processing of read content
JObject job = (JObject)(JsonConvert.DeserializeObject(retString));
var value = job [key].ToString();

Note: JArray for arrays and JObject for objects

Topics: JSON encoding