首页 > 解决方案 > Mono 中的 C# FileStream - 文件共享冲突

问题描述

我有一个在带有 Mono 的 Raspbian 上运行的 C# WinForms 应用程序。它有一个计时器。当 OnTimedEvent 触发时,我检查我是否对要上传的文件具有独占访问权限(以确保它已完成写入磁盘),然后尝试上传。如果上传成功,我将文件移动到存档文件夹,否则我将其保留在那里并等待下一个计时器事件。连接到 Internet 时我没有问题,但是当我没有进行测试并且我的上传失败时,第二个 OnTimedEvent 在检查同一个文件是否准备好(再次)时出现异常。我正进入(状态 :

Error message: ***Sharing violation on path 'path'
***HResult: ***-2147024864

检查文件是否准备好的方法:

 public static bool IsFileReady(string filename)
    {

        // If the file can be opened for exclusive access it means that the file
        // is no longer locked by another process.
        try
        {
            var inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None);

            bool test = inputStream.Length > 0;

            inputStream.Close();
            inputStream.Dispose();

            return test;
        }
        catch (Exception e)
        {
            //log
            throw e;
        }
    }

这是在 OntimedEvent 上执行的:

            var csvFiles = from f in di.GetFiles()
                        where f.Extension == ".csv"
                        select f;  //get csv files in upload folder

            foreach (var file in csvFiles)
            {
                if (IsFileReady(file.FullName))  //check that file is done writing before trying to move.
                {
                    bool IsUploadSuccess = await WritingCSVFileToS3Async(file);//.Wait();  //upload file to S3

                    if (IsUploadSuccess)
                    {
                        File.Move(file.FullName, archivePath + file.Name);  //move to completed folder if upload successful. else, leave there for next upload attempt
                    }
                }
            }

据我所知,当第二个事件触发时,我的第一个 FileStream (File.Open) 似乎仍然锁定了文件。但是,我已将 .Close() 和 .Dispose() 添加到 IsFileReady 方法,但这似乎不起作用。

任何帮助,将不胜感激!

编辑:下面是 WritingCSVFileToS3Async 方法。

static async Task<bool> WritingCSVFileToS3Async(FileInfo file)
    {
        try
        {
            client = new AmazonS3Client(bucketRegion);

            // Put the object-set ContentType and add metadata.
            var putRequest = new PutObjectRequest
            {
                BucketName = bucketName,
                Key = file.Name,
                FilePath = file.FullName ,
                ContentType = "text/csv"
            };
            //putRequest.Metadata.Add("x-amz-meta-title", "someTitle"); //don't need meta data at this time

            PutObjectResponse response = await client.PutObjectAsync(putRequest);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                return true;
            else
                return false;
        }
        catch (AmazonS3Exception e)
        {
            ErrorLogging.LogErrorToFile(e);
            return false;
        }
        catch (Exception e)
        {
            ErrorLogging.LogErrorToFile(e);
            return false;
        }

另外,我在 Windows 上运行了相同的应用程序,并且遇到了类似的异常:

The process cannot access the file 'path' because it is being used by another process.

标签: c#filefile-iotimermono

解决方案


我相信我找到了问题所在。我注意到我没有捕获 PUT 请求的客户端超时异常(未连接到互联网)。我的计时器间隔为 20 秒,比 S3 客户端超时(30 秒)短。因此,在第二个计时器事件触发时,客户端仍然将文件捆绑在一起,因此存在访问冲突。我将定时器间隔增加到 60 秒,现在我捕获了客户端超时异常,并且可以在下一个定时器事件之前处理它。

谢谢你的帮助。


推荐阅读