How to remember a variable at each application startup? [duplicate]

流过昼夜 提交于 2021-02-05 12:33:51

问题


complete noob here, i thought i'd follow a tutorial for a web browser to help teach me some stuff. i've done that, and it works, but now i want to get more advanced with it.

Ive got a tab page control, one tab is browser, and one is bookmarks. i want the bookmarks section to be interactive. so, to add in subtabs for the bookmarks tab page. i want to give the user control, to add/remove tabs. but most importantly, add bookmark buttons.

so my thought was to call an If function, to check a variable on any button clicked.

String UrlCheck; If (UrlCheck => "")

But im stuck. i need this variable remembered the next time this application is executed so setting UrlCheck to "" won't work.

Any ideas how i would go about this? please?


回答1:


You can use application settings behavior provided by the Framework and Visual Studio designer using the settings.settings file in the properties section of the project in the solution explorer.

You create a parameter of string type for example MyStringParameter and you read and write access it like that:

  • Settings are automatically loaded at the program startup.

  • Somewhere to modify or read the value:

    strValueYouNeed = Properties.Settings.Default.MyStringParameter;
    // ...
    Properties.Settings.Default.MyStringParameter = strValueYouWant;
    
  • Somewhere just after the parameter modified or at the program end or form closed event:

    Properties.Settings.Default.Save();
    

Or you can use a hand-made application settings file where you can put what you want by code as well as control the folder location:

How to create a hand-made application settings file

How to initialize user app data and document path

You can also use a simple text file to store a collection.

If it is a string list you simply write:

var list = new List<string>();

// ...

File.WriteAllLines(strPath, list.ToArray());

WriteAllLines takes in fact a string[] as second argument.

To read it, you write:

var list = File.ReadAllLines(strPath)/*.ToList()*/;


来源:https://stackoverflow.com/questions/58577254/how-to-remember-a-variable-at-each-application-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!