C# 文件访问错误

标签 c# ftp file-access

我有以下 C# 代码:

string selectedFile = "D:\..\IMGP2695.JPG";
MyImage myImage = new MyImage();
Image image = Image.FromFile(selectedFile);
string thumbnail_name = CreateThumbnail(image);
myImage.height = image.Height + "px";
myImage.width = image.Width + "px";
ftp.upload(myImage.internalName, selectedFile, directory);


 The process cannot access the file `'D:\..\IMGP2695.JPG'` because it is being used by another process.

当代码尝试运行ftp.upload时,我收到此错误。我知道错误意味着什么,但我不知道如何关闭已打开文件的句柄。

最佳答案

尝试通过使用using包装Image.FromFile处置图像对象:

string selectedFile = "D:\..\IMGP2695.JPG";
MyImage myImage = new MyImage();
string thumbnail_name = string.empty;
using(Image image = Image.FromFile(selectedFile)){
   thumbnail_name = CreateThumbnail(image);
   myImage.height = image.Height + "px";
   myImage.width = image.Width + "px";
}
ftp.upload(myImage.internalName, selectedFile, directory);

关于C# 文件访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15943642/

相关文章:

javascript - Webgl FTP错误

python - 确认 Python 2.6 ftplib 不支持 Unicode 文件名?备择方案?

android - 如何在可绘制或原始文件夹中制作文件的文件对象?

c# - Entity Framework 查询的奇怪行为

powershell - 使用 FTP 将文件发布到 Azure Web App 时出错

grails - groovy中的多文件访问(Groovy on Grails)

iphone - 如何解决iphone开发中遇到的EXC_BAD_ACCESS错误

c# - FileUpload 控件不接受 0kb 文件?

c# - 转义 C#,有点困惑

c# - Angular 应用程序适用于 http ://localhost:4200, 但我想要 https://localhost:44325