kaze
kaze

Reputation: 4359

Create file without opening/locking it?

Does anyone know of a way to (reasonably simple) create a file without actually opening/locking it? In File class, the methods for file creation always return a FileStream. What I want to do is to create a file, rename it (with File.Move) and then use it.

Now I have to:

Upvotes: 8

Views: 9590

Answers (5)

Ravindra Mehta
Ravindra Mehta

Reputation: 36

Another way is to use FileStream and Close it after creating the file. It will not lock the file. The code will look like:

FileStream fs = new FileStream(filePath, FileMode.Create);

fs.Flush(true);

fs.Close();

You just after this you can rename it as well or move it some other location.

Below is the Test program to test functionality.

using System; 
using System.Collections.Generic; 
using System.IO; using
System.Linq;
using System.Text; 
namespace FileLocking {
class Program
{
    static void Main(string[] args)
    {
        string str = @"C:\Test\TestFileLocking.Processing";
        FileIOTest obj = new FileIOTest();
        obj.CreateFile(str);
    }
}

class FileIOTest
{
    internal void CreateFile(string filePath)
    {
        try
        {
            //File.Create(filePath);

            FileStream fs = new FileStream(filePath, FileMode.Create);
            fs.Flush(true);
            fs.Close();

            TryToAccessFile(filePath);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    void TryToAccessFile(string filePath)
    {
        try
        {
            string newFile = Path.ChangeExtension(filePath, ".locked");
            File.Move(filePath, newFile);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
} }

If you use File.Create(commented in above code) then it will give error saying file is being used by another process.

Upvotes: 2

Bolek Tekielski
Bolek Tekielski

Reputation: 1214

Incredibly grotty hack, probably the most complicated way to achieve your goal: use Process class

processInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
processInfo.CreateNoWindow = true; 
processInfo.UseShellExecute = false;
process = process.Start(processInfo);
process.WaitForExit();

where Command would be echo 2>> yourfile.txt

Upvotes: 1

using (File.Create(...))  { }

While this will briefly open your file (but close it again right away), the code should look quite unobtrusive.

Even if you did some P/Invoke call to a Win32 API function, you would get a file handle. I don't think there's a way to silently create a file without having it open right afterwards.

I think the real issue here is why you go about creating your file in the way you've planned. Creating a file in one place simply to move it to another location doesn't seem very efficient. Is there a particular reason for it?

Upvotes: 4

Adriaan Stander
Adriaan Stander

Reputation: 166576

Maybe you can try using File.WriteAllText Method (String, String) with the file name and an empty string.

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Upvotes: 11

Rubens Farias
Rubens Farias

Reputation: 57996

What about using File.WriteAllBytes method?

// Summary:
//     Creates a new file, writes the specified byte array to the file, and then
//     closes the file. If the target file already exists, it is overwritten.

Upvotes: 2

Related Questions