I have this code snippet where i am trying to write in the registry of windows so that i can expire a trail application.
public RegistryKey rootKey;
public RegistryKey regKey;
public long expiry;
// Use this for initialization
void Start()
{
int period = 21; // trial period
string keyName = "/Datefile.txt";
long ticks = DateTime.Today.Ticks;
rootKey = Registry.CurrentUser;
regKey = rootKey.OpenSubKey(keyName);
if (regKey == null) // first time app has been used
{
regKey = rootKey.CreateSubKey(keyName);
expiry = DateTime.Today.AddDays(period).Ticks;
regKey.SetValue("expiry", expiry, RegistryValueKind.QWord);
regKey.Close();
}
else
{
expiry = (long)regKey.GetValue("expiry");
regKey.Close();
long today = DateTime.Today.Ticks;
if (today > expiry)
{
Debug.Log("Application has expired.");
Application.Quit();
}
}
}
But it is giving me this error
ArgumentException: Type does not match the valueKind Microsoft.Win32.Win32RegistryApi.SetValue . . .
Even i have tried to set Object in this line but it producing the same error
regKey.SetValue("expiry", new Expiry(), RegistryValueKind.QWord);
This is only valid for DWord which is 32 bit.
You are trying to put long value into a 32-bit binary number. Which is not going to work.
You can cast the long to unsigned int.
regKey = rootKey.CreateSubKey(keyName);
expiry = DateTime.Today.AddDays(period).Ticks;
var uintVal = (uint)expiry;
regKey.SetValue("expiry", uintVal, RegistryValueKind.DWord);
regKey.Close();
Now when converting back you just:
long myTicks = Convert.ToInt64(myRegValue);
Edit:
Behind the scene QWord is stored as string, you can try to store it as string directly but I suspect this is not your problem
void Start()
{
int period = 21; // trial period
string keyName = "/Datefile.txt";
long ticks = DateTime.Today.Ticks;
rootKey = Registry.CurrentUser;
regKey = rootKey.OpenSubKey(keyName);
if (regKey == null) // first time app has been used
{
regKey = rootKey.CreateSubKey(keyName);
expiry = DateTime.Today.AddDays(period).Ticks;
regKey.SetValue("expiry", expiry.ToString(), RegistryValueKind.String);
regKey.Close();
}
else
{
var s = regKey.GetValue("expiry").ToString();
expiry = long.Parse(s);
regKey.Close();
long today = DateTime.Today.Ticks;
if (today > expiry)
{
//Debug.Log("Application has expired.");
//Application.Quit();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.