我发现自己这么做只是为了确保文件名不被使用.有没有更好的办法?
Directory.Exists(name) || File.Exists(name)
Run Code Online (Sandbox Code Playgroud)
Pau*_*ulG 56
当然:)
internal static bool FileOrDirectoryExists(string name)
{
return (Directory.Exists(name) || File.Exists(name));
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*edt 34
请注意,您所使用的事实是否存在()来检查文件或目录名使用受竞争条件.
在Exists()测试通过后的任何时候,例如,在代码到达创建文件的位置之前,某些东西可能已经创建了具有该名称的文件.
(我假设文件已经存在是一个特例条件).
简单地打开文件,指定适当的FileShare参数更可靠.
例:
using System;
using System.IO;
static class FileNameInUse
{
static void Main(string[] args)
{
string path = args[0];
using (var stream = File.Open(path, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
// Write to file
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,简单地处理失败时的IOException可能会导致更简单的代码更不容易出现竞争条件,因为现在:
FileMode.CreateNew则会导致IOException抛出该文件FileShare.None关闭文件之前,没有其他进程可以访问该文件.不幸的是,如果没有一些丑陋的P/Invoke ,就不可能检查文件当前是否正在使用,而不是抛出异常:
bool IsFileInUse(string fileName)
{
IntPtr hFile = Win32.CreateFile(fileName, Win32.FILE_READ_DATA, 0, IntPtr.Zero, Win32.OPEN_EXISTING, Win32.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
if (hFile.ToInt32() == Win32.INVALID_HANDLE_VALUE)
return true;
Win32.CloseHandle(hFile);
return false;
}
class Win32
{
const uint FILE_READ_DATA = 0x0001;
const uint FILE_SHARE_NONE = 0x00000000;
const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
const uint OPEN_EXISTING = 3;
const int INVALID_HANDLE_VALUE = -1;
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern IntPtr CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll")]
internal static extern bool CloseHandle(IntPtr hObject);
}
Run Code Online (Sandbox Code Playgroud)
这种快速检查也容易出现竞争条件,除非你从中返回文件句柄,并将其传递给相关的FileStream构造函数.
| 归档时间: |
|
| 查看次数: |
75348 次 |
| 最近记录: |