Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# webclient无法使用文件(正在被其他进程使用…)_C#_.net_Winforms - Fatal编程技术网

C# webclient无法使用文件(正在被其他进程使用…)

C# webclient无法使用文件(正在被其他进程使用…),c#,.net,winforms,C#,.net,Winforms,好的……这段代码的一部分很可能使我想要的文件变得繁忙,我需要释放资源,否则webclient或其他任何东西都无法使用该文件: WebClient webClient = new WebClient(); string remote = "sample.jpg"; string px = Request.PhysicalApplicationPath.ToString(); if (File.Exists(px+"1.jpg") != true) { string local =

好的……这段代码的一部分很可能使我想要的文件变得繁忙,我需要释放资源,否则webclient或其他任何东西都无法使用该文件:

WebClient webClient = new WebClient(); 
string remote = "sample.jpg"; 
string px = Request.PhysicalApplicationPath.ToString(); 
if (File.Exists(px+"1.jpg") != true) 
{ 
    string local = px + "1.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
else 
{ 
    string local = px + "2.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
try
{
    byte A, R, G, B;
    Color pixelColor;
    Color pixelColor1;

    string rt = px + "1.jpg";
    string rt1 = px + "2.jpg";

    System.Drawing.Image a = System.Drawing.Image.FromFile(rt);
    Bitmap bitmapImage = new Bitmap(a);

    System.Drawing.Image a1 = System.Drawing.Image.FromFile(rt1);
    Bitmap bitmapImage1 = new Bitmap(a1);



    List<string> list = new List<string>();


    for (int y = 0; y < bitmapImage.Height; y++)
    {
        for (int x = 0; x < bitmapImage.Width; x++)
        {
            pixelColor = bitmapImage.GetPixel(x, y);
            pixelColor1 = bitmapImage1.GetPixel(x, y);
WebClient-WebClient=new-WebClient();
string remote=“sample.jpg”;
字符串px=Request.PhysicalApplicationPath.ToString();
if(File.Exists(px+“1.jpg”)!=true)
{ 
字符串local=px+“1.jpg”;
webClient.DownloadFile(远程、本地);
} 
其他的
{ 
字符串local=px+“2.jpg”;
webClient.DownloadFile(远程、本地);
} 
尝试
{
字节A、R、G、B;
彩色像素;
彩色像素Color1;
字符串rt=px+“1.jpg”;
字符串rt1=px+“2.jpg”;
System.Drawing.Image a=System.Drawing.Image.FromFile(rt);
位图bitmapImage=新位图(a);
System.Drawing.Image a1=System.Drawing.Image.FromFile(rt1);
位图bitmapImage1=新位图(a1);
列表=新列表();
对于(int y=0;y
我得到这个错误

第168行:webClient.DownloadFile(远程、本地);“[IOException:进程无法访问该文件


问题是
webclient
仍然挂在您的文件上

尝试处置webclient,以便释放其资源

WebClient webClient = new WebClient(); 
string remote = "sample.jpg"; 
string px = Request.PhysicalApplicationPath.ToString(); 
if (File.Exists(px+"1.jpg") != true) 
{ 
    string local = px + "1.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
else 
{ 
    string local = px + "2.jpg"; 
    webClient.DownloadFile(remote, local); 
} 
webClient.Dispose()

知道它很旧,但试着用block代替

using (WebClient client = new WebClient())
{
   client.DownloadFile(remotePath, localPath);
}

我不太明白你的问题。你有一个文件被这段代码使用,但是你可以用覆盖来修复它?你需要我们做什么?这是错误:“第168行:webClient.DownloadFile(远程,本地);”[IOException:进程无法访问文件哪一行是第168行?@paqogomez-谢谢你的评论。不,我的意思是我可以通过完整的代码重写来解决这个问题。但我想要更简单的解决方案。例如…如何处理和发布什么。我猜图形对象“bitmapobject”使用的是jpeg图像“2.jpg”"然后我不能在webclient中使用它?再次使用10x。10x-只是尝试了一下,但同样的事情。我忘了提到:第一次代码工作得很好。我的意思是..代码被包装在一个计时器控件中-第一次计时器滴答滴答的时候,一切都很好。但是很明显,我没有关闭图形对象中的某个流,它下面的块是t他为任何进一步的成功计时,在第二次计时时,我遇到了异常。是的,这个问题确实隐藏在处理中-但再次强调,它不是webclient,而是图形。基本上,这似乎解决了它:bitmapImage.Dispose();bitmapImage1.Dispose();a.Dispose();a1.Dispose();因此,处理bitmapimage和“a”将释放“2.jpg”以供进一步使用。我之前错了,这会破坏对象,因为问题实际上是我的浏览器缓存第一次出现“2.jpg”并且不会检测到任何重复更改的图片。因此,我必须修复浏览器图像缓存问题。我现在99%确定一切正常,但需要进行3-4次测试,以查看算法中的所有内容是否正确。我将在30分钟左右后再次写入,并将您的答案标记为最佳。