相关疑难解决方法(0)

有没有办法检查文件是否正在使用?

我正在用C#编写一个需要重复访问1个图像文件的程序.大部分时间它都可以工作,但如果我的计算机运行速度很快,它会在将文件保存回文件系统之前尝试访问该文件并抛出错误:"另一个进程正在使用的文件".

我想找到解决这个问题的方法,但是我所有的谷歌搜索都只是通过使用异常处理来创建检查.这违背了我的宗教信仰,所以我想知道是否有人有更好的方法呢?

.net c# file-io file file-locking

809
推荐指数
11
解决办法
49万
查看次数

Wait for file to be freed by process

How do I wait for the file to be free so that ss.Save() can overwrite it with a new one? If I run this twice close together(ish), I get a generic GDI+ error.

///<summary>
/// Grabs a screen shot of the App and saves it to the C drive in jpg
///</summary>
private static String GetDesktopImage(DevExpress.XtraEditors.XtraForm whichForm)
{
    Rectangle bounds = whichForm.Bounds;

    // This solves my problem but creates a clutter issue
    // var timeStamp = DateTime.Now.ToString("ddd-MMM-dd-yyyy-hh-mm-ss");
    // var fileName …
Run Code Online (Sandbox Code Playgroud)

c# file-io ioexception winforms

32
推荐指数
4
解决办法
7万
查看次数

什么Win32 API可用于查找打开给定文件的进程?

如果我读取或写入文件并收到33或32错误,我想记录一条消息,其中包含打开文件的其他进程的名称.必须有一个Win32 API,我可以使用它来获取此信息.Process Explorer显示它.当然,Process Explorer也有关于内存中所有进程的信息.我宁愿在没有询问所有过程的情况下找到罪魁祸首.

api winapi file

10
推荐指数
1
解决办法
4057
查看次数

等待文件可用于使用Win32进行读取

我正在通过ReadDirectoryChangesW同步调用来查看目录.当一个新文件可用时,我尝试CreateFile使用GENERIC_READ和立即访问它FILE_SHARE_READ,但这给了我ERROR_SHARING_VIOLATION.将文件放入监视目录的过程在我尝试读取它时没有完成写入.

有没有办法可靠地等到文件可供读取?我可以将方法放入如下所示的循环中,但我希望有更好的方法.

while ((hFile = CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
{
    if (GetLastError() == ERROR_SHARING_VIOLATION)
        Sleep (500);
    else
        break; // some other error occurred
}

if (hFile == INVALID_HANDLE_VALUE)
{
    // deal with other error
    return 0;
}

ReadFile (...);
Run Code Online (Sandbox Code Playgroud)

c file-io winapi synchronous

8
推荐指数
2
解决办法
3432
查看次数

标签 统计

file-io ×3

c# ×2

file ×2

winapi ×2

.net ×1

api ×1

c ×1

file-locking ×1

ioexception ×1

synchronous ×1

winforms ×1