0

我有一个应用程序使用 C://root 文件夹。此文件夹包含图像。这些图像由 Pictureboxes 读取。

  1. 图像通过 FTP 来自一台 linux 机器。一台 linux 机器使用 ftp 打开 Windows 中的 C://root。保存 IMAGE_1.jpg。
  2. IMAGE_1.jpg 由 C# windows 窗体应用程序读取以显示在图片框中。

图像每 10 秒从 linux 机器传来。Windows 在一个线程中显示这些图像。

有时,linux 或 windows 会发送异常。因为其中一个试图读取图像以显示(windows),另一个试图保存图像(linux)

因此,我必须明白,如果Image_1.jpg是linux机器使用的,不要试图以win形式显示图像。

但是怎么做?

4

1 回答 1

1

在您的 Win Forms 应用程序中,打开文件进行读取,并与其他进程共享文件,以便他们可以继续读取/写入文件。

使用File.Open 方法(String、FileMode、FileAccess、FileShare)来执行此操作。

如果您只使用File.Open Method (String, FileMode)File.Open Method (String, FileMode, FileAccess),则文件将被取消共享。

通过共享,您应该让 ftp 方面的事情保持愉快。

如果您在尝试在 Windows 中打开文件时遇到异常,那很好。只需捕获异常并尽快重试。

成功打开文件后,检查最后两个字节是否为 FF D9。在这种情况下,您的 JPG 已完成上传。

这是一些伪代码。


    success = false
    using (FileStream fs = File.Open(path,  // eg your Image1.jpg
                                     FileMode.Open,
                                     FileAccess.Read, // we just need to read
                                     FileShare.ReadWrite)) // important to share!
    {
       // if last two bytes are FF D9 then
       //    success = true... can display image now
    }
    if (!success)
    {
       // file is being uploaded, or some other problem occurred
       // try again later
    }

于 2013-03-21T11:21:10.690 回答