简体   繁体   中英

c# how do i prevent one specific character from being added to a text file

I have a program where the user enters data into a text box, and then presses a button which adds it to a text file using stream writer. How do I make an error appear when the user tries to enter the character "~". It breaks the entire program, since its used to seperate different data in the text files.

Check the user input string if it contains the character that you want to disallow. Like below,

string userInput= myTextBox.Text;
if( userInput.Contains('~') )
    MessageBox.Show("This ~ char is not allowed");
...
//write to file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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