使用WebClient UploadFile线程被中止

时间:2018-06-19 15:15:42

标签: c# c#-4.0 webclient

当我写入文件并将同一文件上传到FTP站点时,出现Thread was being aborted错误。

 FileIoUtility.WriteDataToFile(skillsSummaryData, SkillSummaryFileLocation);

                if (!IsFileLocked(SkillSummaryFileLocation))
                {
                    using (WebClient myWebClient = new WebClient())
                    {
                        myWebClient.Credentials = new NetworkCredential(FtpUsername, FtpPassword);
                        var uri = new Uri($"{FtpHost}SkillSummary.txt", UriKind.RelativeOrAbsolute);
                        byte[] responseArray = myWebClient.UploadFile(uri, WebRequestMethods.Ftp.UploadFile,
                            SkillSummaryFileLocation);
                        var response = Encoding.ASCII.GetString(responseArray);
                    }
                }

这是我的WriteDataToFile方法:

public static void WriteDataToFile(string data, string fileLocation, bool append = false)
        {
            if (!File.Exists(fileLocation))
            {
                File.Create(fileLocation).Dispose();
            }

            lock (Locker)
            {
                using (FileStream file =
                    new FileStream(fileLocation, FileMode.Append, FileAccess.Write, FileShare.Read))
                {
                    using (StreamWriter writer = new StreamWriter(file, Encoding.Unicode))
                    {
                        writer.Write(data);
                    }
                }
            }
        }

如果我跳过以下行并运行上载,那么它将起作用。

FileIoUtility.WriteDataToFile(skillsSummaryData, SkillSummaryFileLocation);

除了给出的线程被中止以外,异常没有其他作用。我该如何工作?

0 个答案:

没有答案