Question : Error: The process cannot access the file because it is being used by another process.

hi,

I am using "FtpWebRequest" class to upload a file to a remote server. The below code invokes the Upload process and then delete the file after it is uploaded. I have also attached the code that asynchronously upload a file. The file is getting uploaded fine but when it gets to deleting the file it is throwing the exception "There was a problem deleting the file {0} located in {1}. The process cannot access the file because it is being used by another process".
I think my code tries to delete the file after it is uploaded. Can anybody please let me know where it is going wrong? Thank you.

bool status = AsynchronousFtpUpLoader.Upload(ipaddr, fileName, user, pwd);

                    if (status)
                    {
                        try
                        {
                            if (File.Exists(fileName))
                            {
                                File.Delete(fileName);
                            }
                            else
                            {
                                MessageBox.Show(string.Format("The file {0} does not exist in the location {1}.", Path.GetFileName(fileName), Path.GetDirectoryName(fileName)));
                            }

                        }
                        catch (Exception x)
                        {
                            MessageBox.Show(string.Format("There was a problem deleting the file {0} located in {1}. \r\n\r\n {2}", Path.GetFileName(fileName), Path.GetDirectoryName(fileName), x.Message));
                        }
                    }


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Windows.Forms;

namespace FTPProcess
{
    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
    {
        public static bool Upload(string uploadUrl, string fileName, string username, string pwd)
        {
            ManualResetEvent waitObject;

            try
            {
                string uri = string.Format("ftp://{0}/{1}", uploadUrl, Path.GetFileName(fileName));

                FtpState state = new FtpState();
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.EnableSsl = false;

                request.Credentials = new NetworkCredential(username, pwd);
                state.Request = request;
                state.FileName = fileName;

                waitObject = state.OperationComplete;
                request.BeginGetRequestStream(new AsyncCallback(EndGetStreamCallback), state);
                waitObject.WaitOne();

                if (state.OperationException != null)
                {
                    throw state.OperationException;
                }

                return true;
            }
            catch (Exception x)
            {
                MessageBox.Show(string.Format("Error while uploading the file {0}. \r\n{1}", fileName, x.Message));
                return false;
            }

        }

        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 readBytes = 0;
                FileStream stream = File.OpenRead(state.FileName);
                do
                {
                    readBytes = stream.Read(buffer, 0, bufferLength);
                    requestStream.Write(buffer, 0, readBytes);
                }
                while (readBytes != 0);
                requestStream.Close();
                // 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)
            {
                MessageBox.Show("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)
            {
                MessageBox.Show("Error getting response.");
                state.OperationException = e;
                state.OperationComplete.Set();
            }
        }
    }
}

Answer : Error: The process cannot access the file because it is being used by another process.

As you most likely will not be assigning SIP addresses with the domain.local namespace you'll only need to support the domain.com SIP namespace.  And since there is no mention of an Edge server I'll assume you are planning an internal-only deployment with CWA published to the Internet to allow for browser-based client access for external users.  Thus, here is a general overview of the minimum number of components you would require:

1. A single SSL SAN certificate for the Standard Edition server with the Common Name set to the server's FQDN (e.g. ocsserver1.domain.local) and a single SAN entry of 'sip.domain.com'.

2. A single SSL SAN certificate for the Communicator Web Access (CWA) server. See this thread for more details on using a single certificate for both MTLS and IIS usage on the CWA server:
http://blogs.pointbridge.com/Blogs/schertz_jeff/Pages/Post.aspx?_ID=75

Self-signed certificates cannot be used for OCS and will not work.
Random Solutions  
 
programming4us programming4us