【问题标题】:C# File handling - creating a file and openingC# 文件处理 - 创建文件并打开
【发布时间】:2013-03-07 12:45:02
【问题描述】:

这是我在文件上创建和写入的内容:

    Create_Directory = @"" + path;
    Create_Name = file_name;

    private void Create_File(string Create_Directory, string Create_Name )
    {
        string pathString = Create_Directory;
        if (!System.IO.Directory.Exists(pathString)) { System.IO.Directory.CreateDirectory(pathString); }

        string fileName = Create_Name + ".txt";
        pathString = System.IO.Path.Combine(pathString, fileName);
        if (!System.IO.File.Exists(pathString)) { System.IO.File.Create(pathString); }

        ///ERROR BE HERE:
        System.IO.StreamWriter file = new System.IO.StreamWriter(pathString);
        file.WriteLine(Some_Method(MP.Mwidth, MP.Mheight, MP.Mtype, "" )); 
        file.Close();
    }

我一整天都在努力解决的问题是在创建文件后写入文件。所以,我的程序创建一个文件就好了,然后在写入之前给出一个错误:

“在 mscorlib.dll 中发生了 'System.IO.IOException' 类型的未处理异常”

“附加信息:该进程无法访问文件 'D:\Projects\Project 15\Project 15\world\world maps\A.txt',因为它正被另一个进程使用。”

有趣的是,当我再次运行程序并尝试创建一个已经存在的文件时,如您所见,它会跳过文件创建,开始编写并且工作正常,我真的希望我的程序创建文件无需重新运行即可编写...我在这里看不到什么? :S

【问题讨论】:

    标签: c# file file-io directory streamwriter


    【解决方案1】:

    问题是File.Create 返回一个打开的Stream,而你永远不会关闭它。该文件在您创建 StreamWriter 时处于“使用中”(由您)。

    话虽如此,您不需要“创建”文件。 StreamWriter 会自动为您完成。只需删除此行:

       if (!System.IO.File.Exists(pathString)) { System.IO.File.Create(pathString); }
    

    一切都应该按照书面规定进行。

    请注意,为了更安全,我会稍微改写一下:

    private void Create_File(string directory, string filenameWithoutExtension )
    {
        // You can just call it - it won't matter if it exists
        System.IO.Directory.CreateDirectory(directory);
    
        string fileName = filenameWithoutExtension + ".txt";
        string pathString = System.IO.Path.Combine(directory, fileName);
    
        using(System.IO.StreamWriter file = new System.IO.StreamWriter(pathString))
        {
            file.WriteLine(Some_Method(MP.Mwidth, MP.Mheight, MP.Mtype, "" )); 
        }
    }
    

    您也可以只使用File.WriteAllText 或类似方法来避免以这种方式创建文件。使用using 块保证文件将被关闭,即使Some_Method 引发异常。

    【讨论】:

    • 我可以在这里看到很多有用的建议,我会尝试一下,我认为这样可以:)
    • 一个问题,如果我的文件已经存在并且我尝试创建另一个具有相同名称的文件会发生什么?编辑:我发现,它只会重写它,幸运的是我设法将我的旧代码与你的代码融合在一起,并找到了适合我的东西,谢谢大家:D
    【解决方案2】:

    您可以使用 File 类,因为它为您完成了很多工作

    例子:

    private void Create_File(string Create_Directory, string Create_Name)
    {
        string pathString = Create_Directory;
        if (!System.IO.Directory.Exists(pathString)) { System.IO.Directory.CreateDirectory(pathString); }
    
        pathString = System.IO.Path.Combine(pathString, Create_Name + ".txt");
        File.WriteAllText(fileName, Some_Method(MP.Mwidth, MP.Mheight, MP.Mtype, ""));
    }
    

    【讨论】:

      【解决方案3】:
       static void Main(string[] args)
              {
                  FileStream fs = new FileStream("D:\\niit\\deep.docx", FileMode.Open, FileAccess.Read);
                  StreamReader sr = new StreamReader(fs);
                  sr.BaseStream.Seek(0, SeekOrigin.Begin);
                  string str = sr.ReadLine();
                  Console.WriteLine(str);
                  Console.ReadLine();
      
              }
      

      【讨论】:

      • 上述程序用于读取和打开计算机内部已经创建的文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 2010-09-13
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多