Store a single character (tab, new line, any chara

2019-08-28 03:50发布

I want to store and save back a single character in User.config (or App.config) file. Normal characters can be hold easily, but when it comes to special characters like \t, \n I cannot store them because VS2012 says it is not a character. I modify the User.config file with XML tags of \t which is 	 it works! However, when it comes to save it again, it fails. When I assign \t character like this in code Properties.Settings.Default.delim = '\t' and call Save method, program only stores \0 character. I cannot store XML equivalent string in code because it is a string, not a character and I'm trying to assign a string to a character.

Are there any thoughts on this?

1条回答
干净又极端
2楼-- · 2019-08-28 03:56

You could store the decimal ASCII code for the character by converting to an int, and convert it back to a char in your program.

e.g.

char charToSave = '\t';
int code = (int)charToSave;
SaveCharToUserConfig(code);

...

int code = GetCodeFromUserConfig(); 
char savedChar = (char)code;
查看更多
登录 后发表回答