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

Time:04-11

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.

CodePudding user response:

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
  • Related