C#文件处理-创建文件并打开 [英] C# File handling - creating a file and opening

查看:201
本文介绍了C#文件处理-创建文件并打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我创建并写入文件的过程:

This is what I did to create and write on my file:

    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();
    }

我整天都在奋斗的问题是在创建文件后写入文件.因此,我的程序会很好地创建一个文件,然后在编写之前给出错误:

The problem here, which I have battled the entire day, is writing the file after I create it. So, my program creates a file just fine, then gives out an error before writing:

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

"An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll"

其他信息:该进程无法访问文件'D:\ Projects \ Project 15 \ Project 15 \ world \ world maps \ A.txt',因为它正在被另一个进程使用."

"Additional information: The process cannot access the file 'D:\Projects\Project 15\Project 15\world\world maps\A.txt' because it is being used by another process."

但是,有趣的是,当我再次运行该程序并尝试创建一个已经存在的文件时,如您所见,它跳过了文件创建过程,开始编写并运行良好,我真的希望我的程序来创建文件无需重新运行就可以写...我在这里看不到什么? :S

Funny thing though, when I run the program again and try to create an already existing file, as you can see, it skips file creating, goes to writing and works fine, and I would really want my program to create the file and write without having to rerun it... What am I not seeing here?? :S

推荐答案

问题是 File.Create 返回打开的Stream,而您永远不会关闭它.创建StreamWriter时,文件正在使用"(由您使用).

The problem is that File.Create returns an opened Stream, and you're never closing it. The file is "in use" (by you) at the point in time when you create your StreamWriter.

话虽如此,您无需创建"文件. StreamWriter将自动为您完成此操作.只需删除此行:

That being said, you don't need to "create" the file. StreamWriter will automatically do it for you. Just remove this line:

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

一切都应按书面规定进行.

And everything should work as written.

请注意,我将对此稍作重写,以使其更安全:

Note that I would rewrite this slightly, however, to make it safer:

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引发异常,文件也将关闭.

You can also just use File.WriteAllText or similar methods to avoid creating the file this way. Using the using block guarantees the file will be closed, even if Some_Method raises an exception.

这篇关于C#文件处理-创建文件并打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆