【问题标题】:Creating text file in C#在 C# 中创建文本文件
【发布时间】:2015-03-05 14:28:52
【问题描述】:

我正在学习如何在 C# 中创建文本文件,但我遇到了一个问题。我使用了这段代码:

private void btnCreate_Click(object sender, EventArgs e)        
{

    string path = @"C:\CSharpTestFolder\Test.txt";
    if (!File.Exists(path))
    {
        File.Create(path);
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.WriteLine("The first line!");
        }

    }
    else if (File.Exists(path))
        MessageBox.Show("File with this path already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

当我按下“创建”按钮时,Visual Studio 显示错误“System.IO.DirectoryNotFoundException”,它指向“File.Create(path)”。

问题出在哪里?

【问题讨论】:

  • C:\CSharpTestFolder 是否存在?如果您创建它,您的代码是否有效?您是否具有编辑文件夹的适当权限?
  • 不,此文件不存在。当我手动创建此路径并再次运行程序时,它显示相同的错误,但该路径中的“test.txt”文件是由程序生成的,但是当我打开它时,没有文本。我不确定,但我认为它有编辑权限。

标签: c# file path streamwriter


【解决方案1】:

异常表明您的目录C:\CSharpTestFolder 不存在。 File.Create 将在现有文件夹/路径 中创建一个文件,它也不会创建完整路径。

您的支票File.Exists(path) 将返回false,因为目录不存在,文件也不存在。您需要先检查文件夹上的Directory.Exists,然后创建目录,然后创建文件。

将您的文件操作附在try/catch 中。您不能 100% 确定 File.ExistsDirectory.Exists,可能存在其他创建/删除项目的过程,如果您仅依赖这些检查,您可能会遇到问题。

您可以像这样创建目录:

string directoryName = Path.GetDirectoryName(path);
Directory.CreateDirectory(directoryName);

(您可以在不调用Directory.Exists 的情况下调用Directory.CreateDirectory,如果该文件夹已经存在则不会抛出异常) 然后检查/创建您的文件

【讨论】:

【解决方案2】:

你必须先创建目录。

string directory = @"C:\CSharpTestFolder";

if(!Directory.Exists(directory))
    Directory.CreateDirectory(directory);

string path = Path.Combine(directory, "Test.txt");
if (!File.Exists(path))
{
    File.Create(path);
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine("The first line!");
    }

}
else if (File.Exists(path))
    MessageBox.Show("File with this path already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

【讨论】:

    【解决方案3】:

    假设您的目录存在(如您所说),那么您还有另一个问题

    File.Create 一直锁定它创建的文件,您不能以这种方式使用 StreamWriter。

    相反,你需要写

    using(FileStream strm = File.Create(path))
    using(StreamWriter sw = new StreamWriter(strm))
        sw.WriteLine("The first line!");
    

    但是,除非您需要使用特定选项 (see File.Create overload list) 创建文件,否则所有这些都不是必需的,因为如果文件不存在,StreamWriter 会自行创建文件。

    // File.Create(path);
    using(StreamWriter sw = new StreamWriter(path))
        sw.WriteLine("Text");
    

    ...或全部在一行

    File.WriteAllText(path, "The first line");
    

    【讨论】:

      【解决方案4】:

      试试这个。

      string path = @"C:\CSharpTestFolder";
      
      if (Directory.Exists(path))
      {
          File.AppendAllText(path + "\\Test.txt", "The first line");
      }
      
      else
      {
          Directory.CreateDirectory(path);
          File.AppendAllText(path + "\\Test.txt", "The first line");
      }
      

      File.AppendAllText(path, text) 方法会在不存在的情况下创建一个文本文件;附加文本并将关闭文件。 如果文件已经存在,它将打开文件并将文本附加到文件中,然后关闭文件。

      异常显示目录C:\CSharpTestFolder不存在。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 2012-02-28
        • 2017-03-06
        相关资源
        最近更新 更多