开发者

Create file without opening/locking it?

开发者 https://www.devze.com 2022-12-17 14:02 出处:网络
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 cre

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:


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.


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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消