简体   繁体   English

C#中的文件操作

[英]File Manipulation in C#

如何检查C#中的以下语句是否正确执行?

StreamReader sr = new StreamReader(Path_To_File);

If it didn't throw an exception, it executed correctly. 如果它没有抛出异常,它就会正确执行。 If it throws an exception, it's reasonable to expect the cosntructor to tidy up after itself. 如果它抛出异常,那么期望cosntructor自行整理是合理的。 Otherwise, it'll be up to you to call Dispose on it when you're finished, to release the associated resources. 否则,完成后,您可以在其上调用Dispose ,以释放相关资源。 As others answers have said, you almost certainly want to use a using statement to accomplish this. 正如其他人的回答所说的那样,你几乎肯定希望使用using语句来实现这一目标。

You might also want to use File.OpenText instead: 您可能还想使用File.OpenText

using (TextReader reader = File.OpenText(fileName))
{
}

I only usually use the StreamReader constructor when I need to pass in different options (which is pretty rarely). 当我需要传递不同的选项时,我通常只使用StreamReader构造函数(这很少)。

If it didn't, I would expect it to throw an exception. 如果没有,我希望它会抛出异常。 So do nothing; 所以什么都不做; it will tell you if there is a problem. 它会告诉你是否有问题。 But you should be " using " it: 但你应该“ using ”它:

using(StreamReader sr = new StreamReader(Path_To_File))
{
    // consume sr
}

Because StreamReaders implement IDisposable, you can use a 'using' block. 由于StreamReaders实现了IDisposable,因此您可以使用“使用”块。

using(StreamReader sr = new StreamReader(Path_To_File)) {

}

I may be missing something in the question, because this seems too obvious, but the two things to look for are 我可能会在问题中遗漏一些东西,因为这看起来太明显了,但需要注意的两件事情

  1. Did it throw an error? 它输错了吗? and
  2. When you run this, and read using the StreamReader, are you getting the content you expect? 当您运行此程序并使用StreamReader读取时,您是否获得了预期的内容?

If 1 is false and 2 is true, it executed correctly. 如果1为假且2为真,则执行正确。

The StreamReader constructor (assuming a string path argument) will throw an exception if it fails. StreamReader构造函数 (假设字符串路径参数)如果失败则抛出异常。

Quoted from the link: 引自链接:

ArgumentException ArgumentException的
path is an empty string (""). path是一个空字符串(“”)。

ArgumentNullException ArgumentNullException
path is null. path为null。

FileNotFoundException FileNotFoundException异常
The file cannot be found. 找不到该文件。

DirectoryNotFoundException DirectoryNotFoundException
The specified path is invalid, such as being on an unmapped drive. 指定的路径无效,例如位于未映射的驱动器上。

IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label. IOException路径包含文件名,目录名或卷标的错误或无效语法。

You'd want to check for exceptions using standard Try / Catch blocks like this: 您需要使用标准的Try / Catch块检查异常,如下所示:

string pathToFile = @"G:\My Documents\donkeysex.txt";

StreamReader sr = null;

try
{
    sr = new StreamReader(pathToFile);
    sr.Read();
    // etc.
}
catch (System.IO.FileNotFoundException ex)
{
    // Handle exception
}
catch (System.IO.DirectoryNotFoundException ex)
{
    // Handle exception
}
catch (System.IO.IOException ex)
{
    // Handle exception
}
catch (Exception ex)
{
    // Handle exception
}
finally
{
    if (sr != null)
        sr.Dispose();
}

If you just want to ensure the file exists before reading then use: 如果您只是想在阅读之前确保文件存在,那么使用:

if (System.IO.File.Exists(pathToFile))
{
    // Do your stuff
}

You would normally do somthing with the instance sr. 你通常会对实例sr做一些事情。 Encapsulate your next command which references that object in a try catch block. 封装您在try catch块中引用该对象的下一个命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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