在代码中使用streamreader和streamwriters进行登录系统时(可以创建新帐户并可以登录现有帐户)我一直在使用:
usernameReader = New StreamReader("J:\Computing Coursework\real project\KES\Resources\username.txt")流读取器的“新”部分是必要的(因为变量不能仅仅等于流),但是我想知道是否还有其他选项可以使用而不是'as new streamreader'来打开流而不删除用户名文本文件的内容。
任何帮助,将不胜感激。
When using streamreader and streamwriters in code for a login system (where new accounts can be created and existing accounts can be logged into) I have been using:
usernameReader = New StreamReader("J:\Computing Coursework\real project\KES\Resources\username.txt")The 'New' part of the streamreader is necessary (as the variable cannot equal just the stream), however I was wondering if there were any other options that I could use instead of the 'as new streamreader' to open the stream without erasing the content of the username text file.
Any help would be appreciated.
最满意答案
如果您不需要为更具体的方案配置StreamReader,也可以使用File.OpenText 。 File类为常见的I / O任务提供了几个有用的快捷方式。
顺便说一句:我不认为打开StreamReader会删除文件; 我宁愿怀疑写入文件的代码是错误的。 如果您打开文件进行写入,它将删除该文件:
writer = new StreamWriter("... path ...")看看File.AppendText或类似的东西。
You can also use File.OpenText if you do not need to configure the StreamReader for a more specific scenario. The File class provides several useful shortcuts for common I/O tasks.
By the way: I don't think that opening a StreamReader erases the file; I'd rather suspect that the code that writes to the files is wrong. It will erase the file if you open the file for writing like this:
writer = new StreamWriter("... path ...")Have a look at File.AppendText or something similar.
更多推荐
发布评论