如何在程序崩溃时不会删除的文本框中保存文本? [英] How do I save text in a textbox that wont delete when the program crashes?

查看:89
本文介绍了如何在程序崩溃时不会删除的文本框中保存文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计

我有一个c#应用程序,要求用户在第一次运行程序时输入他的名字。我们希望始终保存此用户名,直到他决定删除该程序。我想保存它,因为有一个选项可以将日志文件发送给开发人员,我们需要知道它来自谁。我正在使用winforms。



我尝试了什么:



i尝试将其保存在一个读取文本的文件中,如果不同则保存但是如果程序被visual studio关闭,它就不会保存它。

Hi folks
i have a c# application that requires the user tom type his name in when he first runs the program. We wish to save this username always until he decides to delete the program. I want to make it that it saves it because there is an option of sending the log file to the developers and we need to know who it came from.I am using winforms.

What I have tried:

i tried saving it in a file that reads the text and if it is different it saves it but it does not save it if the program gets closed by visual studio.

推荐答案

最简单的方法是当用户输入它时,将其保存到应用程序设置中:

1)在解决方案窗格中,打开项目分支。

2)打开Properties twig,双击Settings.Settings

3)在显示的页面中:

3.1)在名称下输入用户名

3.2)在类型下选择字符串

3.3)在范围下选择用户

3.4)在值下,将其留空。

4)关闭页面,并保存更改。 />
5)现在在启动时的代码中执行以下操作:

Easiest way is when the user enters it, save it into the application settings:
1) In the solution pane, open the project branch.
2) Open the Properties twig, and double click "Settings.Settings"
3) In the page that is displayed:
3.1) Under "Name" enter "UserName"
3.2) Under "Type" select "String"
3.3) Under "Scope" select "User"
3.4) Under "Value" leave it blank.
4) Close the page, and save the changes.
5) Now in your code at startup do this:
string userName = Properties.Settings.Default.UserName;
if (string.IsNullOrWhiteSpace(userName))
    {
    ... Insert your code to get the user name ...
    Properties.Settings.Default.UserName = userName;
    Properties.Settings.Default.Save();
    }


当我读到的东西时,真正的问题是你想要向开发人员发送带有错误日志的用户名。更好的方法是捕获他们的登录名,而不是让他们输入一些文本,然后保存它。您可以从Environment.UserName获取他们的登录名。



更好的解决方案是使用log4net或nLog等产品来捕获有关错误的信息。这些产品允许您将信息保存到文件或数据库或两者。这些是您可以添加到解决方案中的块包。
As I read things the real issue is that you want to send a users name with an error log to the developers. The better approach is to capture their login name instead of having them type in some text and then saving it. you can get their login name from Environment.UserName.

A better solution is to use a product like log4net or nLog to capture information about the error. These products allow you to save the information to a file or a database or both. These are nugget packages that you can add to your solution.


这篇关于如何在程序崩溃时不会删除的文本框中保存文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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