views:

249

answers:

3

Hi all,

I am trying to get a matlab-compiled exe running on Azure cloud, and for that purpose need to get a v78.zip onto the local storage of the cloud and unzip it, before I can try to run an exe on the cloud. The program works fine when executed locally, but on deployment gives and error at line marked below in the code. The error is :

The process cannot access the file 'C:\Resources\directory\cc0a20f5c1314f299ade4973ff1f4cad.WebRole.LocalStorage1\v78.zip' because it is being used by another process.

Exception Details: System.IO.IOException: The process cannot access the file 'C:\Resources\directory\cc0a20f5c1314f299ade4973ff1f4cad.WebRole.LocalStorage1\v78.zip' because it is being used by another process.

The code is given below:

        string localPath = RoleEnvironment.GetLocalResource("LocalStorage1").RootPath;

        Response.Write(localPath + " \n");

        Directory.SetCurrentDirectory(localPath);

        CloudBlob mblob = GetProgramContainer().GetBlobReference("v78.zip");
        CloudBlockBlob mbblob = mblob.ToBlockBlob;

        CloudBlob zipblob = GetProgramContainer().GetBlobReference("7z.exe");

        string zipPath = Path.Combine(localPath, "7z.exe");
        string matlabPath = Path.Combine(localPath, "v78.zip");
        IEnumerable<ListBlockItem> blocklist = mbblob.DownloadBlockList();


        BlobStream stream = mbblob.OpenRead();
     FileStream fs = File.Create(matlabPath);    (Exception occurs here)

It'll be great help if someone could tell me where I'm going wrong.

Thanks! Shan

A: 

Add a using clause around most of this. You have a file handle to your zip file hanging around. When the using goes out of scope, so will the file reference.

using(CloudBlob mblob = GetProgramContainer().GetBlobReference("v78.zip"))
{
        CloudBlockBlob mbblob = mblob.ToBlockBlob;

        CloudBlob zipblob = GetProgramContainer().GetBlobReference("7z.exe");

        string zipPath = Path.Combine(localPath, "7z.exe");
        string matlabPath = Path.Combine(localPath, "v78.zip");
        IEnumerable<ListBlockItem> blocklist = mbblob.DownloadBlockList();


        BlobStream stream = mbblob.OpenRead();
}
 FileStream fs = File.Create(matlabPath);
Dan
Hi Dan, Thanks for the suggestion. I am facing 2 issues implementing it - First, using expects a type implicitly convertible to System.IDisposable and second, there are declarations and assignments happening inside the using stmt u have put (in code above) which are referenced outside the using. That wont work right? Am I missing something here?
Shantanu
A: 

CloudBlob's are not IDisposable, so you don't need a using statement. (They're just references, so they don't allocate any resources that need to be released.)

You should, however, probably have a using() block around the FileStream. Are you sure it's being closed?

Where is this code running? Is it run only once?

BTW, you can just do "container.GetBlobRefence("foo").DownloadToFile(matlabPath);" (anticipating what you're about to do with that file handle).

smarx
A: 

Try fs.close() once you are done with it.

Sunipa