疑难解答:读取和写入文本文件 (Visual Basic)Troubleshooting: reading from and writing to text files (Visual Basic)

本主题讨论处理文本文件时遇到的常见问题,并针对每个问题提供建议解决方法。This topic discusses common problems encountered when working with text files and suggests an approach to each.

常见问题Common problems

处理文本文件时遇到的最常见问题包括安全异常、文件编码和无效路径。The most common issues encountered when working with text files include security exceptions, file encodings, or invalid paths.

安全异常Security exceptions

安全错误发生时引发 SecurityExceptionA SecurityException is thrown when a security error occurs. 这通常是由用户缺少必要权限导致的,通过添加权限或者在独立存储中处理文件可以解决该问题。This is often a result of the user lacking necessary permissions, which may be solved by adding permissions or working with files in isolated storage.

文件编码File encodings

文件编码也称为字符编码,用于指定在处理文本时如何表示字符。File encodings, also known as character encodings, specify how to represent characters when text processing. 文本文件中的意外字符可能是由于不正确的编码导致的。Unexpected characters in a text file may result from incorrect encoding. 对于大多数文件,一种编码可能优于另一种编码主要取决于它能处理或不能处理哪些语言字符,不过通常首选的是 Unicode。For most files, one encoding may be preferable over another in terms of which language characters it can or cannot handle, although Unicode is usually preferred. 有关详细信息,请参阅文件编码EncodingFor more information, see File Encodings and Encoding.

错误路径Incorrect paths

分析文件路径尤其是相对路径时,很容易提供错误的数据。When parsing file paths, particularly relative paths, it is easy to supply the wrong data. 请确保提供正确的路径,这样可以纠正许多问题。Many problems can be corrected by making sure you are supplying the correct path. 有关详细信息,请参阅如何:分析文件路径For more information, see How to: Parse File Paths.

另请参阅See also