c# - Getting int from registry converted from string to int -


i've got program has remember user had located last time got closed. chose attempt registry, not know how convert value gotten registry.read int can set location form. here's have that:

    modifyregistry myregistry = new modifyregistry(); (< field variable)       private void robcsvkopieren_load(object sender, eventargs e)     {         location.x = ((int)myregistry.read("location.x"));     }      private void robcsvkopieren_locationchanged(object sender, eventargs e)     {      }      private void robcsvkopieren_formclosing(object sender, formclosingeventargs e)     {         myregistry.write("location.x", location.x);         myregistry.write("location.y", location.y);         myregistry.write("size", size);     } 

ps: experience registry absolutely 0, code registry modification site found multiple times on site.

pps. here modify registry class' code (you might notice haven't changed @ since afraid break it.:

/* ***************************************   *           modifyregistry.cs  * ---------------------------------------  *         simple class   *    read, write, delete , count  *       registry values c#  * ---------------------------------------  *      if improve code   *   please email me improvement!  * ---------------------------------------  *         francesco natali  *        - fn.varie@libero.it -  * ***************************************/  using system; // it's required reading/writing registry: using microsoft.win32; // , messagebox function: using system.windows.forms;  namespace utility.modifyregistry { /// <summary> /// useful class read/write/delete/count registry keys /// </summary> public class modifyregistry {     private bool showerror = false;     /// <summary>     /// property show or hide error messages      /// (default = false)     /// </summary>     public bool showerror     {         { return showerror; }         set { showerror = value; }     }      private string subkey = "software\\" + application.productname.toupper();     /// <summary>     /// property set subkey value     /// (default = "software\\" + application.productname.toupper())     /// </summary>     public string subkey     {         { return subkey; }         set { subkey = value; }     }      private registrykey baseregistrykey = registry.localmachine;     /// <summary>     /// property set baseregistrykey value.     /// (default = registry.localmachine)     /// </summary>     public registrykey baseregistrykey     {         { return baseregistrykey; }         set { baseregistrykey = value; }     }      /* **************************************************************************      * **************************************************************************/      /// <summary>     /// read registry key.     /// input: keyname (string)     /// output: value (string)      /// </summary>     public string read(string keyname)     {         // opening registry key         registrykey rk = baseregistrykey;         // open subkey read-only         registrykey sk1 = rk.opensubkey(subkey);         // if registrysubkey doesn't exist -> (null)         if (sk1 == null)         {             return null;         }         else         {             try             {                 // if registrykey exists value                 // or null returned.                 return (string)sk1.getvalue(keyname.toupper());             }             catch (exception e)             {                 // aaaaaaaaaaargh, error!                 showerrormessage(e, "reading registry " + keyname.toupper());                 return null;             }         }     }      /* **************************************************************************      * **************************************************************************/      /// <summary>     /// write registry key.     /// input: keyname (string) , value (object)     /// output: true or false      /// </summary>     public bool write(string keyname, object value)     {         try         {             // setting             registrykey rk = baseregistrykey;             // have use createsubkey              // (create or open if exits),              // 'cause opensubkey open subkey read-only             registrykey sk1 = rk.createsubkey(subkey);             // save value             sk1.setvalue(keyname.toupper(), value);              return true;         }         catch (exception e)         {             // aaaaaaaaaaargh, error!             showerrormessage(e, "writing registry " + keyname.toupper());             return false;         }     }      /* **************************************************************************      * **************************************************************************/      /// <summary>     /// delete registry key.     /// input: keyname (string)     /// output: true or false      /// </summary>     public bool deletekey(string keyname)     {         try         {             // setting             registrykey rk = baseregistrykey;             registrykey sk1 = rk.createsubkey(subkey);             // if registrysubkey doesn't exists -> (true)             if (sk1 == null)                 return true;             else                 sk1.deletevalue(keyname);              return true;         }         catch (exception e)         {             // aaaaaaaaaaargh, error!             showerrormessage(e, "deleting subkey " + subkey);             return false;         }     }      /* **************************************************************************      * **************************************************************************/      /// <summary>     /// delete sub key , child.     /// input: void     /// output: true or false      /// </summary>     public bool deletesubkeytree()     {         try         {             // setting             registrykey rk = baseregistrykey;             registrykey sk1 = rk.opensubkey(subkey);             // if registrykey exists, delete             if (sk1 != null)                 rk.deletesubkeytree(subkey);              return true;         }         catch (exception e)         {             // aaaaaaaaaaargh, error!             showerrormessage(e, "deleting subkey " + subkey);             return false;         }     }      /* **************************************************************************      * **************************************************************************/      /// <summary>     /// retrive count of subkeys @ current key.     /// input: void     /// output: number of subkeys     /// </summary>     public int subkeycount()     {         try         {             // setting             registrykey rk = baseregistrykey;             registrykey sk1 = rk.opensubkey(subkey);             // if registrykey exists...             if (sk1 != null)                 return sk1.subkeycount;             else                 return 0;         }         catch (exception e)         {             // aaaaaaaaaaargh, error!             showerrormessage(e, "retriving subkeys of " + subkey);             return 0;         }     }      /* **************************************************************************      * **************************************************************************/      /// <summary>     /// retrive count of values in key.     /// input: void     /// output: number of keys     /// </summary>     public int valuecount()     {         try         {             // setting             registrykey rk = baseregistrykey;             registrykey sk1 = rk.opensubkey(subkey);             // if registrykey exists...             if (sk1 != null)                 return sk1.valuecount;             else                 return 0;         }         catch (exception e)         {             // aaaaaaaaaaargh, error!             showerrormessage(e, "retriving keys of " + subkey);             return 0;         }     }      /* **************************************************************************      * **************************************************************************/      private void showerrormessage(exception e, string title)     {         if (showerror == true)             messagebox.show(e.message,                             title                             , messageboxbuttons.ok                             , messageboxicon.error);     } } } 

is conversion int that's problem here? if yes should use:

location.x = convert.toint32(myregistry.read("location.x")); 

note: if it's not int value, formatexception avoid can use:

int locationx; var success = int32.tryparse(myregistry.read("location.x"), out locationx); if(success)  location.x = locationx; else  location.x = // defaultvalue 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -