How to properly create a blank file in C#?

I'm trying to create a blank text file in one of my onclick events. This is what I did:

string someName = @"C:\Temp\test.txt";

                if (!File.Exists(someName))
                {
                    try
                    {
                        File.CreateText(someName);                        
                    }
                }

It creates the file but I can't open it. It says it's being used by another process. Seems that it's locked. It has the right inherited permissions so it's not that. Do I have to do something extra? Another method need to write to it shortly after and am having trouble.
bemara57Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
This method creates a StreamWriter, which you are not closing. So, try the code below, and it should be OK.

Jim

try
{
  StreamWriter sw = File.CreateText(someName);
  sw.Close();                        
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rahul Goel ITILSenior Consultant - DeloitteCommented:
use this code
using(TextWriter temp = new StreamWriter(@":\Temp\test.txt",FileMode.Create){}

It will create the file and will close that time as well.

0
xPert_UmerCommented:

Using sw As StreamWriter = New StreamWriter("newfile.txt")
            ' Add some text to the file.
            sw.Write("This is the ")
            sw.WriteLine("header for the file.")
            sw.WriteLine("-------------------")
            ' Arbitrary objects can also be written to the file.
            sw.Write("The date is: ")
            sw.WriteLine(DateTime.Now)
            sw.Close()
        End Using

Open in new window

0
bemara57Author Commented:
Thanks all, it worked great. I ended up using:

                        using (StreamWriter sw = File.CreateText(file))
                        { sw.Close(); }

I figured this would take care of the dispose as well. Although I don't think I need to explicitly close it since the using theoretically takes care of it. By the way, do I have to do this for copying files as well? Because I also have this in my code where I need to write to file2 shortly after:

File.Copy(file1, file2, false);
0
JimBrandleyCommented:
That one does not return a stream, so you will be OK.

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.