3

In sharepoint from Document library i was read the file and Download the file using FileStream object. Sometime it was downloading. Sometime i will receive the following error.

The process cannot access the file 'c:\windows\system32\inetsrv\ because it is being used by another process.

My code :

 SPListItem item = DownLoadfile(strID, strListID, "AllowEmailShare");



            if (item != null)
            {
                byte[] obuffer;
                SPFile stFile = item.File;

                if (stFile.LockType != SPFile.SPLockType.None)

                {

                    stFile.ReleaseLock(stFile.LockId);

                }
                //IsFileinUse(oSPFile)
                // if (!IsFileLocked(oSPFile))
                if (stFile != null)
                {


                    //obuffer = oSPFile.OpenBinary();
                    using (FileStream sr = new FileStream(stFile.Name, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                    {

                        byte[] Data = stFile.OpenBinary(); //in this l ine i was receiving the error
                        if (Data != null)
                        {
                            sr.Write(Data, 0, Data.Count());
                            sr.Close();
                            sr.Dispose();
                            string strUrl = stFile.Web.Url + "/" + stFile.Url;

                            this.Page.Response.Clear();
                            this.Page.Response.ContentType = "application/zip";
                            this.Page.Response.AddHeader("content-disposition", "Attachment; filename=" + Convert.ToString(item["Name"]));
                            //Write the file directly to the HTTP content output stream.
                            //BinaryWriter bw = new BinaryWriter(outStream);
                            //bw.Write(fileData);
                            //bw.Close();

                            this.Page.Response.BinaryWrite(Data);
                            this.Page.Response.Flush();
                            this.Page.Response.Close();

                        }
                    }
                }
            }
0

We can check if the handle of the file is being used to achieve it.

Here is the code of how to check handle of file for your reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationCommon
{

    /// <summary>
    /// Description of UseAPI.
    /// </summary>
    public class UseAPI
    {

        private const int OF_READWRITE = 2;
        private const int OF_SHARE_DENY_NONE = 0x40;
        private static readonly IntPtr HFILE_ERROR = new IntPtr(-1);


        /// <summary>
        /// get the handle of file
        /// </summary>
        /// <param name="lpPathName">file path</param>
        /// <param name="iReadWrite"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern IntPtr _lopen(string lpPathName, int iReadWrite);

        /// <summary>
        /// close file handle
        /// </summary>
        /// <param name="hObject"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(IntPtr hObject);

        /// <summary>
        /// check if the file is being used
        /// </summary>
        /// <param name="filePath">file path</param>
        /// <returns></returns>
        public static bool IsOpen(string filePath)
        {
            IntPtr vHandle = _lopen(filePath, OF_READWRITE | OF_SHARE_DENY_NONE);
            if (vHandle == HFILE_ERROR)
            {
                CloseHandle(vHandle);
                return true;
            }
           else
            {
                CloseHandle(vHandle);
                return false;
            }              
        }
    }

}

Screenshot of result: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.