3 回答

TA貢獻1794條經驗 獲得超8個贊
我只是想以雙引號格式保留變量
抱歉,您似乎對變量和數據類型的工作方式有錯誤的看法。整數變量只是一塊沒有引號或其他格式的 32 位內存。
您根本無法將雙引號文本分配或解析(沒有自定義解析字符串)到整數。停止嘗試。改用這個:
if (int.TryParse(textBox1.Text, out int password))
{
// text is a valid int, use `password`
}

TA貢獻1847條經驗 獲得超7個贊
你真的想密碼是整數而已?用戶只允許有123密碼,但不能,比如說,a;sldf123_'vdkdm?更自然的選擇是獲取密碼(讓它在textBox2.Text),因為它是:
string fileName = Path.Combine(
@"C:\Users\Cavid\Desktop\LogFiles", // Directory
$"{DateTime.Now.ToString("dd.MM.yyyy")}.txt"); // File Name
// Do not call DateTime.Now twice: you may have different times for username and password
DateTime moment = DateTime.Now;
string prefix = $"{moment.ToShortDateString()} {moment.ToShortDateString()}";
// It seems you want textBox2.Text, not textBox1.Text as a password (typo?)
string[] lines = new string[] {
$"{prefix} \"{textBox1.Text}\" has typed as username",
$"{prefix} \"{textBox2.Text}\" has typed as password",
};
// Append file once
File.AppendAllLines(fileName, lines);
如果您堅持只使用整數作為密碼,請添加int.Parse或int.TryParse:
...
string[] lines = new string[] {
$"{prefix} \"{textBox1.Text}\" has typed as username",
$"{prefix} \"{int.Parse(textBox2.Text)}\" has typed as password",
};
...

TA貢獻1866條經驗 獲得超5個贊
要添加雙引號,請使用以下代碼:
字符串用戶名 = textBox1.Text; //添加雙引號
username = "\"" + username + "\""; //you can test it as shown below
File.WriteAllText(@"C:\text2.txt", 用戶名);
對于錯誤,正如其他人提到的那樣,您無法將所有 string s 轉換為整數(例如,如果您的用戶名是字母數字)
- 3 回答
- 0 關注
- 222 瀏覽
添加回答
舉報