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

标签 c# .net file-io

有谁知道无需实际打开/锁定文件即可(相当简单)创建文件的方法吗?在 File 类中,创建文件的方法总是返回一个 FileStream。我想要做的是创建一个文件,重命名它(使用 File.Move)然后使用它。

现在我必须:

  • 创建它
  • 关闭
  • 重命名
  • 重新打开使用

最佳答案

也许你可以尝试使用 File.WriteAllText Method (String, 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.

关于c# - 创建文件而不打开/锁定它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2116161/

相关文章:

c# - 使用 Microsoft Fakes 填充 SerialPort 对象

c# - 使用 INSERT 语句时如何修复 'SQLite exception, Insufficient parameters supplied to the command'

c# - 为什么这个方法每次都返回相同的随机字符串?

c# - VS Code 无法在 Ubuntu 中找到 .Net 6 - 无法编译和运行应用程序

c# - 从 IEnumerable foreach 循环中获取值

java - 为什么使用 BufferedInputStream 逐字节读取文件比使用 FileInputStream 快?

c - 如何 fread() 结构?

java - 如何在 Camel 进程中访问资源中的文件?

c# - 如何在 C# 中创建返回数据表的类?

c# - 如何使用 LINQ Contains() 查找枚举列表?