【发布时间】:2020-03-30 22:57:06
【问题描述】:
我们有一个在客户端上运行的 C# WinForms 应用程序。应用程序从 FTP 下载文件,将其保存在共享驱动器(托管在服务器上),然后服务器将运行一些代码来解密文件并将解密后的文件发布回共享驱动器,与加密文件,使用不同的文件名。完成后,执行将传递回客户端,并尝试检查解密文件是否存在。然后它会抛出异常,因为找不到文件。
客户端代码:
protected void DecryptFile(string aEncryptedFilePath, string aDecryptedFilePath)
{
AppController.Task.SystemTaskManager.ExecuteTask(new TaskRequest(TaskPgpFileDecrypt.TaskId, aEncryptedFilePath, aDecryptedFilePath));
if (!File.Exists(aDecryptedFilePath)
throw new FileNotFoundException("File does not exist"); // Exception thrown here
}
服务器代码:
public TaskResponse Execute(string aSourceFilePath, string aDestinationFilePath)
{
// Decryption Code
if (!File.Exists(DestinationFilePath))
throw new ApplicationException($"Could not {ActionDescription} file {SourceFilePath}. Expected output file {DestinationFilePath} is missing.");
using (var fileStream = new FileStream(DestinationFilePath, FileMode.Open))
fileStream.Flush(true);
return new TaskResponse();
}
我已尽我所能对其进行了简化,但您可以看到客户端传入了 aEncryptedFilePath 以及它所期望的 aDecrypedFilePath,服务器代码将解密加密文件并存储在路径中存储在aDestinationFilePath。
现在在服务器代码中,您还可以看到我们检查文件是否存在,如果不存在,服务器将抛出异常。但这是踢球者。服务器的文件存在检查返回 true 并继续执行。只有当我们到达客户端代码时,File.Exist 检查才会失败!我们已经尝试刷新缓冲区以确保将文件写入磁盘,但这根本没有帮助。
另一个有用的信息是我可以验证文件是否存在,因为如果我查看创建文件的文件夹,我可以看到它已创建。但是,如果我在文件显示后立即单击该文件,我会从 Windows 收到此警告:
如果我关闭警告并等待一两秒钟,我就可以打开文件了。
什么可能导致这种行为?
【问题讨论】:
-
磁盘延迟???
-
但是为什么会显示文件存在于服务器上呢?
-
可能出现的目录条目与实际可用的文件之间存在差距?
-
请您详细说明一下吗?
标签: c# winforms file server client