我正尝试使用来自文本框的input作为string。 问题是,我需要它只使用它,如果框填充。 我的目标是允许用户在框中input用户名,如果框是空的,我希望它仍然使用我的全局静态string,如果可能的话。 如果有什么更好的想法,我为他们起来
我的程序使用USERNAMEvariables来获取用户名,但是我希望用户能够在需要时在框中input用户名。
string username = (Environment.GetEnvironmentVariable("USERNAME"));
谢谢
public partial class Form1 : Form { //My original strings, This is what i need to fix static string config = File.ReadAllText("config.ini"); static string username = (Environment.GetEnvironmentVariable("USERNAME")); static string Backups = config + @"\" + username + @"\" + "Backups" + @"\"; static string items; public Form1() { InitializeComponent(); } // How would i re purpose the string depending on the if? if (!String.IsNullOrEmpty(toolStripTextBox1.Text)) { toolStripTextBox1.Text = username; MessageBox.Show(username); } else { string username = (Environment.GetEnvironmentVariable("USERNAME")); }
如果toolStripTextBox1
不是空的,你把username
字符串的值放在toolStripTextBox1
里面? 在我看来,像IF和Else这两个值是相同的,就是从配置文件中取得的用户名值。
所以我不知道如果我正确地理解你的问题,但我想你想要的东西是这样的:
if (!String.IsNullOrEmpty(toolStripTextBox1.Text)) { username = toolStripTextBox1.Text; MessageBox.Show(username); // will be the username the user enters in the textbox } else { MessageBox.Show(username); // will be the username taken from your config file }
如果我明白你的正确,这里是你的问题。
if (!String.IsNullOrEmpty(toolStripTextBox1.Text)) { toolStripTextBox1.Text = username;
它应该是
string username = toolStripTextBox1.Text;