none
The process cannot access the file ' blah blah.xml' because it is being used by another process.

    Question

  • I need some serious C# wizard help. I have EVERYTHING working except one piece that got me working on this for more than 4 days...I can not get the following code to let me delete, move, rename, or do anything with the files in the localsource folder files AFTER the FTP upload....Please help me...

     

    The error I get is : "The process cannot access the file 'blah blah' because it is being used by another process."

    ===============================================================

    using System;

    using System.Net;

    using System.Threading;

    using System.IO;

     

    namespace Examples.System.Net

    {

    public class FtpState

    {

    private ManualResetEvent wait;

    private FtpWebRequest request;

    private string fileName;

    private Exception operationException = null;

    string status;

    public FtpState()

    {

    wait = new ManualResetEvent(false);

    }

    public ManualResetEvent OperationComplete

    {

    get { return wait; }

    }

    public FtpWebRequest Request

    {

    get { return request; }

    set { request = value; }

    }

    public string FileName

    {

    get { return fileName; }

    set { fileName = value; }

    }

    public Exception OperationException

    {

    get { return operationException; }

    set { operationException = value; }

    }

    public string StatusDescription

    {

    get { return status; }

    set { status = value; }

    }

    }

    public class AsynchronousFtpUpLoader

    {

    static public string server = @"C:\localsourcefolder\";

    static public string mynewserver = @"C:\localarchivefolder\";

    static public string ftpaddress = @"ftp://www.ftpaddress/";

    // Command line arguments are two strings:

    // 1. The url that is the name of the file being uploaded to the server.

    // 2. The name of the file on the local machine.

    //

    public static void Main()

    {

    DirectoryInfo dir = new DirectoryInfo(server);

    FileInfo[] rgFiles = dir.GetFiles("*.xml");

    foreach(FileInfo fi in rgFiles)

    {

    FtpState state = new FtpState();

    string target = ftpaddress+fi.Name;

    string fileName = server+fi.Name;

    ManualResetEvent waitObject;

    // Create a Uri instance with the specified URI string.

    // If the URI is not correctly formed, the Uri constructor

    // will throw an exception.

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);

    request.Method = WebRequestMethods.Ftp.UploadFile;

    // This example uses anonymous logon.

    // The request is anonymous by default; the credential does not have to be specified.

    // The example specifies the credential only to

    // control how actions are logged on the server.

    request.Credentials = new NetworkCredential("username", "password");

    // Store the request in the object that we pass into the

    // asynchronous operations.

    state.Request = request;

    state.FileName = fileName;

    // Get the event to wait on.

    waitObject = state.OperationComplete;

    // Asynchronously get the stream for the file contents.

    request.BeginGetRequestStream(

    new AsyncCallback(EndGetStreamCallback),

    state

    );

    // Block the current thread until all operations are complete.

    waitObject.WaitOne();

    waitObject.Close();

    }

     

     

     

     

     

     

    FtpState state2 = new FtpState();

    // The operations either completed or threw an exception.

    if (state2.OperationException != null)

    {

    throw state2.OperationException;

    }

    else

    {

    Console.WriteLine("The operation completed - {0}", state2.StatusDescription);

    Thread.Sleep(120000);

    Thread t2 = new Thread(new ThreadStart(AsynchronousFtpUpLoader.cleanup));

    t2.IsBackground = true;

    t2.Priority = ThreadPriority.Highest;

    t2.Start();

    }

    }

    private static void EndGetStreamCallback(IAsyncResult ar)

    {

    FtpState state = (FtpState)ar.AsyncState;

    Stream requestStream = null;

    // End the asynchronous call to get the request stream.

    try

    {

    requestStream = state.Request.EndGetRequestStream(ar);

    // Copy the file contents to the request stream.

    const int bufferLength = 2048;

    byte[] buffer = new byte[bufferLength];

    int count = 0;

    int readBytes = 0;

    FileStream stream = File.OpenRead(state.FileName);

    do

    {

    readBytes = stream.Read(buffer, 0, bufferLength);

    requestStream.Write(buffer, 0, readBytes);

    count += readBytes;

    }

    while (readBytes != 0);

    Console.WriteLine("Writing {0} bytes to the stream.", count);

    // IMPORTANT: Close the request stream before sending the request.

    requestStream.Close();

    requestStream.Dispose();

    // Asynchronously get the response to the upload request.

    state.Request.BeginGetResponse(

    new AsyncCallback(EndGetResponseCallback),

    state

    );

    }

    // Return exceptions to the main application thread.

    catch (Exception e)

    {

    Console.WriteLine("Could not get the request stream.");

    state.OperationException = e;

    state.OperationComplete.Set();

    return;

    }

    }

    // The EndGetResponseCallback method

    // completes a call to BeginGetResponse.

    private static void EndGetResponseCallback(IAsyncResult ar)

    {

    FtpState state = (FtpState)ar.AsyncState;

    FtpWebResponse response = null;

    try

    {

    response = (FtpWebResponse)state.Request.EndGetResponse(ar);

    response.Close();

    state.StatusDescription = response.StatusDescription;

    // Signal the main application thread that

    // the operation is complete.

    state.OperationComplete.Set();

    }

    // Return exceptions to the main application thread.

    catch (Exception e)

    {

    Console.WriteLine("Error getting response.");

    state.OperationException = e;

    state.OperationComplete.Set();

    }

    }

    public static void cleanup()

    {

    DirectoryInfo dir2 = new DirectoryInfo(server);

    FileInfo[] rgFiles2 = dir2.GetFiles("*.xml");

    foreach (FileInfo fi2 in rgFiles2)

    {

    //fi2.CopyTo(mynewserver+fi2.Name + ".archive");

    //fi2.MoveTo(mynewserver + fi2.Name.ToString() + ".archive");

    fi2.Delete();

     

    }

    }

    }

     

    ================================================================

    Monday, March 03, 2008 6:27 AM

Answers

All replies

  • Got it....

     

    had to

    Stream.Close();

     

    Monday, March 03, 2008 6:48 AM
  • I think u need to do a small change in "EndGetStreamCallback" as the following (I just added using)

    Code Snippet

     

    using(FileStream stream = File.OpenRead(state.FileName))

    {

    do

    {

    readBytes = stream.Read(buffer, 0, bufferLength);

    requestStream.Write(buffer, 0, readBytes);

    count += readBytes;

    }

    while (readBytes != 0);

    }

     

     

    Please let me know if it helped.

     

    Good Luck

    Monday, March 03, 2008 6:52 AM