Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webclient cannot use file (being used by another process..)

Tags:

c#

.net

winforms

OK...part of this code MOST LIKELY makes busy the file I want and i need to release resources otherwise the file can't be used by webclient or anything else:

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);

I get this error.

Line 168: webClient.DownloadFile(remote, local);" [IOException: The process cannot access the file

like image 292
user3130269 Avatar asked Feb 25 '26 03:02

user3130269


1 Answers

The problem is that webclient is still hanging on to your file.

Try disposing the webclient so it releases its resources.

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()
like image 179
crthompson Avatar answered Feb 27 '26 18:02

crthompson