简体   繁体   English

很简单的问题-启动C#时自动加载文本框

[英]Very simple question - Autoload textbox when startup C#

The tittle says it all :) I am creating a program in C# forms. 标题说明了这一切:)我正在用C#形式创建程序。 The user can write a text, and save it. 用户可以编写文本并保存。 What i now want, is to autoload this textfile, witch only can be at one location - At the Directory.GetCurrentDIrectory() + "text.txt" . 我现在想要的是自动加载此文本文件,女巫只能位于一个位置Directory.GetCurrentDIrectory() + "text.txt" So how should this be done? 那么应该怎么做呢?

I am assuming that you want to do this when the form is available, in which case I would recommend using the Form Load event . 我假设您想在表单可用时执行此操作,在这种情况下,我建议您使用Form Load事件 Just open the file, read its contents, and set the Text property to the contents in the form load. 只需打开文件,读取其内容,然后将Text属性设置为表单加载中的内容即可。

It's quite simple really: 真的很简单:

//Autoload
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "text.txt");
if(File.Exists(filePath))
    yourTextBox.Text = File.ReadAllText(filePath);

As Ryan points out, you could do it in the FormLoad event. 正如Ryan指出的那样,您可以在FormLoad事件中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM