如何在每次应用程序启动时记住一个变量? [英] How to remember a variable at each application startup?

查看:27
本文介绍了如何在每次应用程序启动时记住一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里完成菜鸟,我想我会按照网络浏览器的教程来帮助教我一些东西.我已经这样做了,并且有效,但现在我想要更高级的使用它.

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.

所以我的想法是调用 If 函数,以检查单击的任何按钮上的变量.

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

字符串网址检查;如果 (UrlCheck => "")

String UrlCheck; If (UrlCheck => "")

但是我卡住了.我需要在下次执行此应用程序时记住此变量,因此将 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?

推荐答案

您可以使用框架和 Visual Studio 设计器提供的应用程序设置行为,使用解决方案资源管理器中项目属性部分中的 settings.settings 文件.

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.

您创建一个字符串类型的参数,例如 MyStringParameter 并像这样读写访问:

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.

在某处修改或读取值:

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:

    如何创建一个手工制作的应用程序设置文件

    如何初始化用户应用数据和文档路径

    您还可以使用简单的文本文件来存储集合.

    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 实际上将 string[] 作为第二个参数.

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

    要阅读它,你写:

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

    这篇关于如何在每次应用程序启动时记住一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆