Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# File.Delete(mapPathName)不';正在使用t工作文件_C#_.net - Fatal编程技术网

C# File.Delete(mapPathName)不';正在使用t工作文件

C# File.Delete(mapPathName)不';正在使用t工作文件,c#,.net,C#,.net,我使用上面的代码从谷歌地图下载了一张加密图像,但下载完成后,我无法删除该文件,因为它正在使用中。 有谁能帮我解决这个问题吗 啊,啊,啊,啊。非常抱歉,我错了。上面的代码起作用了,但当我试图在删除之前绘制时,这一次不起作用 代码如下: WebClient webClient = new WebClient(); string mapPathName = Server.MapPath("\\images\\temp.jpg"); Uri uri = new Uri(mapLink); webClie

我使用上面的代码从谷歌地图下载了一张加密图像,但下载完成后,我无法删除该文件,因为它正在使用中。 有谁能帮我解决这个问题吗

啊,啊,啊,啊。非常抱歉,我错了。上面的代码起作用了,但当我试图在删除之前绘制时,这一次不起作用 代码如下:

WebClient webClient = new WebClient();
string mapPathName = Server.MapPath("\\images\\temp.jpg");
Uri uri = new Uri(mapLink);
webClient.DownloadFile(uri, mapPathName);
webClient.Dispose();
if (File.Exists(mapPathName))
    File.Delete(mapPathName);

您打电话给Dispose有什么特别的原因吗?这可能是原因。如果坚持使用dispose,请在调用dispose之前尝试删除它。例如

PdfDocument document = new PdfDocument();
document.PageLayout = PdfPageLayout.SinglePage;
document.Info.Title = "Created with PDFsharp";

// Create an empty page
PdfPage page = document.AddPage();

// set size
page.Size = PageSize.A4;

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

WebClient webClient = new WebClient();
string mapPathName = Server.MapPath("\\images\\temp.jpg");
Uri uri = new Uri(mapLink);
webClient.DownloadFile(uri, mapPathName);

// defind position to draw the image
XRect rcImage = new XRect(x + 30, y, 410, 300);

// draw the image
gfx.DrawRectangle(XBrushes.Snow, rcImage);
gfx.DrawImage(XImage.FromFile(Server.MapPath("\\images\\temp.jpg")), rcImage);

// save pdf file
string filename = "_HelloWorld.pdf";
string filePath = Server.MapPath(filename);
if (File.Exists(filePath))
    File.Delete(filePath);

document.Save(Server.MapPath(filename));

gfx.Dispose();
page.Close();
document.Dispose();

if (File.Exists(mapPathName))
    File.Delete(mapPathName);

您是否也考虑过使用子句的
?这通常可以防止此类错误的发生。

我解决了我的问题。替代使用

webClient.DownloadFileCompleted += (o, s) =>
{
    //if (File.Exists(mapPathName)) discard, see Paolo's comments below . . .
       File.Delete(mapPathName);
};
webClient.DownloadFileAsync(uri, mapPathName);
webClient.Dispose();
我把它分成两行

gfx.DrawImage(XImage.FromFile(Server.MapPath("\\images\\temp.jpg")), rcImage);
然后

var img = XImage.FromFile(Server.MapPath("\\images\\temp.jpg"));
gfx.DrawImage(img, rcImage);

然后我可以删除图像:D谢谢大家。

这是故意的。如果你想删除一个文件,请停止使用它。@JonathanWood-在上面的代码中它在哪里被使用?你为什么下载它并立即删除它?这是测试代码吗?@Maurice Reeves-是的,这只是测试代码。我将在删除图像之前将其绘制为pdf文件。但是我试着先删除图像。你有没有考虑过有可能是反病毒或其他类似的安全软件在下载后检查代码,以确认它不是威胁?我会考虑暂时禁用反病毒,然后再尝试看看这是否是罪魁祸首。<代码>下载文件> 事件只在使用异步<代码> DownloadFileAsync < /C>方法时可能被调用,因为您仍在下载错误,您试图删除它吗?<代码>下载文件< /代码>方法是一个同步的阻塞操作。方法完成后,文件不可能仍在下载。通常会发生此错误,因为它仍在合成过程中。无论如何,如果它是本地文件,那么您可以始终使用FileWatcher查看更改,直到文件可用为止。在这种情况下使用
File.Exists
是多余的,因为如果要删除的文件不存在,则不会从
File.Delete引发异常。在尝试使用文件之前检查文件是否存在几乎是不合适的(操作不是原子的)。查看更多详细信息。
img.Dispose;