The process cannot access the file because it is being used by another process

Last Reply on Jan 16, 2013 10:36 PM By Mudassar

Posted on Jan 15, 2013 11:38 PM

Hi sir,

I am trying to write data in one file after that i am copying that file to my location.
but i got one error The process cannot access the file , because it is being used by another process.
Please give me sugessions.

public void SaveVcatFile()
        {
            if (!string.IsNullOrEmpty(ApplicationVariables.SaveFilePath))
            {
                if (string.IsNullOrEmpty(GUID))
                {
                    GUID = ApplicationVariables.GuidFolderName;
                }
                DirectoryInfo dir = new DirectoryInfo(path + "/" + GUID + "/");
                var directories = dir.GetFiles("*.vcat", SearchOption.AllDirectories);
                string filePath = path + "/" + GUID + "/" + directories[0].Name;
                CreatrZipFileForSave(path + "/" + GUID);
                Thread.Sleep(6000);
                File.Copy(filePath, ApplicationVariables.SaveFilePath, true); //here I am getting exception 
 
            }
        }
 
 
private void CreatrZipFileForSave(string filePath)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path + "/" + GUID + "/");
            var directories = dirInfo.GetFiles("*.vcat", SearchOption.AllDirectories);
            string VcatFileName = directories[0].Name;
            string zipfilepath = path + "/" + GUID + "/" + VcatFileName;
            FileStream fZip = File.Create(zipfilepath);
            ZipOutputStream zipOStream = new ZipOutputStream(fZip);
            DirectoryInfo dir = new DirectoryInfo(filePath);
            var allfiles = dir.GetFiles("*", SearchOption.AllDirectories);
 
            foreach (FileInfo fi in allfiles)
            {
                if (fi.Extension.ToLower() != ".vcat")
                {
                    ZipEntry entry = new ZipEntry((fi.Name));
                    zipOStream.PutNextEntry(entry);
                    FileStream fs = File.OpenRead(fi.FullName);
                   //FileStream fs = File.Open(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.None);
                    try
                    {
                        do
                        {
                            bytesRead = fs.Read(transferBuffer, 0, transferBuffer.Length);
                            zipOStream.Write(transferBuffer, 0, bytesRead);
                        }
                        while (bytesRead > 0);
                    }
 
                    finally
                    {
                        fs.Close();
                    }
                }
            }
            zipOStream.SetLevel(6);
            zipOStream.Finish();
            zipOStream.Flush();
            zipOStream.Close();
           
            //zipOStream.Flush();
            //Thread.Sleep(1000);
        }

 

Posted on Jan 16, 2013 10:36 PM

There are few problems. Issue is that you are not disposing objects. Hence call

 

zipOStream.Dispose();
fs.Close();
fs.Dispose();