简体   繁体   English

创建文件而不打开/锁定它?

[英]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. 在File类中,文件创建方法始终返回FileStream。 What I want to do is to create a file, rename it (with File.Move) and then use it. 我想要做的是创建一个文件,重命名它(使用File.Move),然后使用它。

Now I have to: 现在我必须:

  • Create it 创造它
  • Close
  • Rename 改名
  • Reopen for use 重新开放使用

Maybe you can try using File.WriteAllText Method (String, String) with the file name and an empty string. 也许你可以尝试使用文件名和空字符串的File.WriteAllText方法(字符串,字符串)

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. 如果目标文件已存在,则会被覆盖。

What about using File.WriteAllBytes method? 那么使用File.WriteAllBytes方法呢?

// 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.
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. 即使您对Win32 API函数执行了一些P / Invoke调用,也会得到一个文件句柄。 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? 这有什么特别的原因吗?

Incredibly grotty hack, probably the most complicated way to achieve your goal: use Process class 令人难以置信的糟糕黑客,可能是实现目标最复杂的方法:使用Process

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 其中Command将是echo 2>> yourfile.txt

Another way is to use FileStream and Close it after creating the file. 另一种方法是在创建文件后使用FileStream并关闭它。 It will not lock the file. 它不会锁定文件。 The code will look like: 代码如下所示:

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

fs.Flush(true); fs.Flush(真);

fs.Close(); 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. 如果您使用File.Create(在上面的代码中注释),那么它将给出错误,说文件正由另一个进程使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM