3

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:

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

"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."

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

3 Answers 3

5

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.

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, "" )); 
    }
}

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can see a lot of usefull advice here, I'll try it out, I think this will do :)
A question though, what will happen if my file already exists and I try to make another one with same name? EDIT: I found out, it would just rewrite it, luckily I've managed to fuse my old code with yours and found something that works for me, thanks all :D
2

You can use the File class as it wraps up a lot of the work for you

Example:

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, ""));
}

Comments

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

        }

1 Comment

above program for read and open the file which are already created inside the computer .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.