Trying to Delete a file gives: The process cannot access the file because it is being used by another process.

Hello, when trying to delete a file I had copied into a file share on a computer I get the following error: 

The process cannot access the file '\\SomeServer\SomeDirrectory\SOmeFIle.ext' because it is being used by another process.

This file was placed in the file share using File.copy() and that seemed to work fine. Now in the second page to this app I want to give the option to delete the file. After having read some posts it seems possible that the file stream is not getting closed after the file.copy()

but, I do not know how to close this stream, or how to find out if that is indeed the problem, any help would be appreciated, thanks  a lot!

0
gobotsoup
8/9/2007 9:03:45 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

6 Replies
932 Views

Similar Articles

[PageSpeed] 48

 For what its worth, I run into this problem fairly often.  It drives me crazy.  I have been searching for well over a year for some good documentation on best practices using System.IO but I have never found anything remotely helpful.

To solve it  i generally have to refactor the app, to use as little file IO code as possible.    

I know that wasn't a very helpful response Sad Good luck.

0
viscious
8/9/2007 10:59:10 PM

while reading or writing to a file specify the fileshare option as "fileshare.readwrite" like,

objFsAction = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) 


Known is handful and unknown is worldful.

Regards,
Ajay Matharu
http://ajaymatharu.com
0
matharuajay
8/10/2007 6:01:44 AM

try this code

        string originalPath = "c:\\somedir\\somefile.ext";
        string copyToPath = "c:\\otherdir\\otherfile.ext";

        using (FileStream file = File.Open(path, FileMode.Open, FileAccess.Read))
        {
            int numOfBytes = file.Length;
            using (FileStream newFile = File.Create(copyToPath, numOfBytes, FileOptions.WriteThrough))
            {
                byte[] bytes = new byte[numOfBytes];
                file.Read(bytes, 0, numOfBytes);
                newFile.Write(bytes, 0, numOfBytes);
                newFile.Flush();
            }
        }

 This would work nice for files that are not very large in size. For bigger files try with some loop statement by reading smaller chunks from the source file and writing them to the destination file. In this way it will take some time but will keep memory free.

I hope this helps


http://pavkata.blogspot.com/
0
mystery
8/10/2007 11:15:58 AM

Hi

This article is fine with the file management.

http://aspalliance.com/920#Page3

hope it will do 

 

0
nshankar
8/10/2007 11:49:03 AM

Thanks for your help, but I am still getting the same error as before. I tried both of the suggestions above. Also, I am now no longer able to even go into the file share to delete them manually, it says another process is using the file.

0
gobotsoup
8/10/2007 4:34:34 PM

Okay I think I have it *mostly* working. It seems I had to call the flush method along with the close method for both file streams (the original and the new). It is interesting because a process seems to be locking it still, but if you wait for a few minutes you will then be able to delete it. I wonder if this is a designed feature or not. It certainly, to me at least, seems quite non-intuitive. :)

0
gobotsoup
8/10/2007 5:11:33 PM
Reply:

Similar Artilces:

Error: The process cannot access the file because it is being used by another process. on File.Open
I have a gridView that has a bunch on document names stored in it. In a template column I have a view button. When the view button is clicked I run some javascript that that opens a new window and passes a documentID. In the viewDocuments form I make a call to the server passing to ID to get document details: Dim cmdSELDoc As New SqlCommand("[get_document]", conHTA) cmdSELDoc.CommandType = Data.CommandType.StoredProcedure Dim prmDocID As SqlParameter = cmdSELDoc.Parameters.Add("@i_document_id", Data.SqlDbType.Int) prmDocID.Value = Request.QueryString("docID") Try reader = cmdSELDoc.Ex...

The process cannot access the file <<File Name>> because it is being used by another process
Hi GuysI am uploading a file to the server and trying to convert the uploaded file into different format. At the time of conversion I am getting above error.upfile.PostedFile.SaveAs(FileName)  ==>File is saved at serverupfile = NothingAny idea how to resolve this problem.Thanks in advance.Sham...

Howto?! - cannot delete a picture with File.delete
Hi guys! I have a problem with asp.net and IIS when i try to delete a picture In an AJAX call a try to delete a picture, that is displayed on my website. When clicking a radiobutton next to the picture, i want to perform a File.Delete(picturepath). However, this is not possible. I always get the message... cannot delete file....is used by another process. Even in Windows i cannot delete this file while the website is running. Folder permissions are to everyone. I heard about that the IIS blocks and caches the files somehow - is there a way i can delete the picture? thank...

Could not write to output file '...\obj\Debug\abc.dll" -- 'The process cannot access the file because it is being used by another process'
I use VS2003. Recently I keep getting this error when building the solution. Any idea how to fix it?? THANKS!  Could not write to output file '...\obj\Debug\abc.dll" -- 'The process cannot access the file because it is being used by another process' Close any other projects that link to this dll (i.e., any extra Visual Studio instances).  Also, try closing the app-- If you are running/debugging the project that references this dll, then you may be using that dll.  ---------------------------------------MCP - Web Based Client Development .NET 2.0 Thanks f...

Cannot copy assembly 'DotNetNuke' to file '<assemly>'. The process cannot access the file because it is being used by another process.
I am in the process of creating a new module and I keep running into this problem. This happens every time I try to build the project. SOMETIMES I can get it working again by deleting the previously generated assemblies and reopening the project, but that doesn't always work and I'd like for the problem not to be there in the first place. My new module project is located at \DotNetNuke\DesktopModules\NewModule The build path for the assembly is set to ..\..\bin which is \DotNetNuke\Bin I am also using a project reference rather than an assembly reference so that shouldn't be the p...

The process cannot access the file,because it is being used by another process.
Hi.. I am working ASP.NET 2.0 + C#. I am using the XmlDocument to read the data from an xml file. As the xml file is used by another thread, when I refresh the page, I sometimes get an error as : An exception of type 'System.IO.IOException' occurred and was caught.---------------------------------------------------------------------Type : System.IO.IOException, mscorlib, Version=2.0.0.0, Culture=neutral, Message : The process cannot access the file 'D:\my.xml' because it is being used by another process. What should be done? How to close the XmlDocument? How to get ...

The process cannot access the file ....because it is being used by another process.
   private void Form1_Load(object sender, EventArgs e)        {            pictureBox1.Image = Image.FromFile("1.jpg");            pictureBox1.Image = Image.FromFile("2.jpg"); ;                    }        private void button1_Click(object sender, EventArgs e)        {  &n...

The process cannot access the file because it is being used by another process
I got this --------------------------------------------------------------------- The process cannot access the file because it is being used by another process. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The process cannot access the file because it is being used by another process. --------------------------------------------------------------------- when I try to move image ...

The process cannot access the file because it is being used by another process?
Hi, I am totally stuck on this one and would really appreciate any help. I am reading and writing xml files in my web app. When I try to open an xml file with any kind of TextReader more than once I get this error. "The process cannot access the file [the file path] because it is being used by another process." I have used the close method and I have also tried Response.clearContent(). Apparently the server can only open a file once? I find this to be really awkward and I'm surprised not to have found a work around for this out there. Has anyone else run into this...

The process cannot access the file because it is being used by another process.
Hi all, I had executing the following piece of code and it always give me the above error. Does someone know what is the problem? Please help      if (Directory.Exists(mydir)) { fs = new FileStream(mydir + myfile, FileMode.Append, FileAccess.Write, FileShare.None); } else { Directory.CreateDirectory(mydir); fs = new FileStream(mydir + myfile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);   } try { sw = new StreamWriter(mydir + myfile); sw.WriteLine("test"); sw.Close(); fs.Close(); } catch (IOException exIO) { //sw.Close(); //fs....

the process cannot access the file because it is being used by another process
 Imports System.IO Imports System.Data Partial Class RTT_SaveAsTxt Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim strfoldername As String = "C:\Txt\" Dim strfilename As String = "C:\Txt\users.txt" If Not Directory.Exists(strfoldername) Then Directory.CreateDirectory(strfoldername) End If If Not File.Exists(strfilename) Then File.Create(strfilename) End If End Sub Protected Sub btnSave...

The process cannot access the file because it is being used by another process.
I've inherited a system w/5 projects - I'll refer to them as P1 thru P5 - P5 being the executable... P1 has no dependents. P2 is dependant upon P1 P3 has no dependents. P4 is dependant upon P1, P2, P3 P5 is dependant upon P1, P2, P3, P4 The build order is: P1, P2, P3, P4, P5 So far so good? I'm setting the 'Build' path of each project to Px\obj\debug with the exception of the main project (P5)- that is being built into its \BIN folder... When I set the necessary references that each project needs to reference, for example, P2 needs a reference to P1, so I'll set...

The process cannot access the file because it is being used by another process.
Hi guys, I am trying to create an HTML file in a directory and then write the text contents from a database to the file.  Unfortunately each time I try I get the above error.  Is there any way to "pause" the system until it has finished with the file and then have it write the text to the file? Here is the code that I have thus far: <code> Dim title as String = Dater &"-"& R("name") &"-"& R("BatchNo") &".htm"                             &...

Process cannot access file because it is being used by another process
I'm using the SharpZipLibrary : http://www.icsharpcode.net/OpenSource/SharpZipLib/. I'm unzipping a zipped folder to a location, and based on some conditions, my code attempts to move that folder to a new location after unzipping.  I'm using the fastzip class to do this, but I keep getting the wonderful "process cannot access file because it is being used by another process."  FastZip doesn't have a dispose method, so I don't know how to get it to release the file, so I can then (maybe) move it.  Does anyone have any ideas for how I can get around this? Thanks ...

Web resources about - Trying to Delete a file gives: The process cannot access the file because it is being used by another process. - asp.net.web-forms

Stationary process - Wikipedia, the free encyclopedia
In the mathematical sciences , a stationary process (or strict(ly) stationary process or strong(ly) stationary process ) is a stochastic process ...

UN adopts Syria peace process that makes no mention of Assad's future
Russia's deputy foreign minister says Security Council members have reached agreement on a resolution they plan to adopt on Friday endorsing ...

Sarkisian Alleges He Was “Kicked To The Curb” By USC: Lessons In Disability Accommodation And The Interactive ...
As an alum of USC Law, I have been particularly interested in the news surrounding USC’s termination of football coach, Steve Sarkisian.

Example of Visualizing Process Cycle Efficiency with LEGO
Learn more about transforming people, process and culture with the Real Agility Program In-depth article here: Using Lego[sic] to capture raw ...

Progress! New CMOS chip can process both light and electricity
... inside chips themselves. This poses a significant challenge. While it's possible to use silicon to create light-handling features, the processes ...

Advanced Development Process with Apps Script
... Apps Script Importing and Exporting Projects page. There are a few things that you should be aware of as you plan out your development process. ...

​U.N. endorses Syria peace process, but no mention of Assad
Draft resolution calls for peace talks between Syrian government and opposition "on an urgent basis"; negotiations could begin next month

U.S. must continue improving the patent process
... to be reimbursed should other patents in the dispute be invalidated, which is likely based on U.S. Patent and Trade Office (USPTO) proceedings ...

Krauthammer: Obama Admin’s Visa Vetting Process Is ‘Incomprehensible’ And ‘Ridiculous’
Krauthammer: Obama Admin’s Visa Vetting Process Is ‘Incomprehensible’ And ‘Ridiculous’

Digitimes Research: South Korea expected to develop large-size flexible displays based on AMOLED, PI ...
... Korea is expected to adopt AMOLED display technology, PI (polyimide) plastic substrates, organic TFT driving technology and roll-to-roll process ...

Resources last updated: 12/25/2015 11:06:38 PM