-1

i want to save Keycode value to config file to load when app starts. Here is my code.

 private void ButtonSelect_KeyDown(object sender, KeyEventArgs e)
    {                       
        buttonValue.Text += (char)e.KeyCode;
        Keys main = e.KeyCode;          
        mainValue = main;             
    }

i read mainValue from another form and i want to save mainValue to config file. thank you very much!

Maximvs
  • 9
  • 2
  • 2
    Why a config file? Use [Application Settings](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings). – 41686d6564 stands w. Palestine Aug 28 '17 at 15:59
  • What config file? What relevance has the fact where you get mainValue from if you overwrite it anyway? What have you tried to do this? This site is not supposed to write entire solutions - you need to show some effort. – Maciej Jureczko Aug 28 '17 at 16:14
  • public partial class ButSelMain : Form { public static string ButMain; public static Keys mainValue; } I want , when KeyDown to show char to label.text and save KeyCode to user.config I have create Properties settings and i save all app's values there and works fine. My problem is that i can't found how can i save e.Keycode value... I try to save it as Convert.ToString , Convert.ToInt.....but not work. I am new to programming and please to show understanting... Thank you again! – Maximvs Aug 28 '17 at 16:27
  • If you want to save the char value of the keycode, create a `Setting` of type `char`, and cast the `KeyCode` value to `char` when saving it (i.e. `(char)e.KeyCode`). If you want to save the numeric value of the keycode, create a `Setting` of type `int`, and cast the `KeyCode` value to `int` when saving it (i.e. `(int)e.KeyCode`). Hope that helps. It's important though to read [how to ask](http://stackoverflow.com/help/how-to-ask), and put some effort in improving the quality of your questions to get better help. – 41686d6564 stands w. Palestine Aug 28 '17 at 16:50
  • SOLVED!!!....On Properties.Settings i load manually `system.windows.forms.Keys` to value that i want to save it as Keys. Values are saved to user.config file and everything works fine! Thank you very much for your help!! – Maximvs Aug 28 '17 at 20:34

1 Answers1

0

I understand your question that way, that you want to write your content to some type of "config file".

The easiest Solution to write content to a file would be :

File.WriteAllText("pathToFileHere", buttonValue.Text)

You could also create a FileStream. As mentioned here

If you want to write to the App.config you can use the code sample provided by this asnwer And for reading from that Config you can use the code provided by that answer

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45