コード例 #1
0
        public static byte[] GetBytesFromUNC(string FileLocation)
        {
            ImpersonateUser iu = new ImpersonateUser();

            byte[] aFileBytes;

            bool bOverrideCredentials = false;

            if (ConfigurationManager.AppSettings["OverrideCredentialsForGetBytesFromUNC"] == "1")
            {
                bOverrideCredentials = true;
            }

            try
            {
                if (bOverrideCredentials)
                {
                    //Impersonate user so we can write to file system (without giving write access to the website's user)
                    iu.Impersonate(ConfigurationManager.AppSettings["g_PublishDomain"], ConfigurationManager.AppSettings["g_PublishUser"], ConfigurationManager.AppSettings["g_PublishPwd"]);
                }

                FileInfo   objFileInfo   = new FileInfo(FileLocation);
                FileStream objFileStream = new FileStream(FileLocation, FileMode.Open, FileAccess.Read);

                //Set buffer size
                aFileBytes = new byte[objFileStream.Length];

                //read data into buffer (i.e. aFileBytes)
                using (BinaryReader objBinaryReader = new BinaryReader(objFileStream))
                {
                    aFileBytes = objBinaryReader.ReadBytes((int)objFileInfo.Length);
                    objBinaryReader.Close();
                }

                objFileStream.Close();
            }

            catch (Exception ex)
            {
                throw new ApplicationException("Error Reading File from File System <br />" + ex.Message + "<br />" + ex.InnerException);
            }
            finally
            {
                if (bOverrideCredentials)
                {
                    iu.Undo();
                }
            }

            return(aFileBytes);
        }
コード例 #2
0
        public static byte[] GetBytesFromUNC(string FileLocation)
        {
            ImpersonateUser iu = new ImpersonateUser();
            byte[] aFileBytes;

            bool bOverrideCredentials = false;
            if (ConfigurationManager.AppSettings["OverrideCredentialsForGetBytesFromUNC"] == "1")
            {
                bOverrideCredentials = true;
            }

            try
            {
                if (bOverrideCredentials)
                {
                    //Impersonate user so we can write to file system (without giving write access to the website's user)
                    iu.Impersonate(ConfigurationManager.AppSettings["g_PublishDomain"], ConfigurationManager.AppSettings["g_PublishUser"], ConfigurationManager.AppSettings["g_PublishPwd"]);
                }

                FileInfo objFileInfo = new FileInfo(FileLocation);
                FileStream objFileStream = new FileStream(FileLocation, FileMode.Open, FileAccess.Read);

                //Set buffer size
                aFileBytes = new byte[objFileStream.Length];

                //read data into buffer (i.e. aFileBytes)
                using (BinaryReader objBinaryReader = new BinaryReader(objFileStream))
                {
                    aFileBytes = objBinaryReader.ReadBytes((int)objFileInfo.Length);
                    objBinaryReader.Close();
                }

                objFileStream.Close();
            }

            catch (Exception ex)
            {
                throw new ApplicationException("Error Reading File from File System <br />" + ex.Message + "<br />" + ex.InnerException);
            }
            finally
            {
                if (bOverrideCredentials)
                {
                    iu.Undo();
                }
            }

            return aFileBytes;
        }
コード例 #3
0
        /// <summary>
        /// Method will save file to file system.  It will overwrite existing files at the same path.
        /// </summary>
        /// <param name="FilePath">Fully qualified File path including name and extension</param>
        /// <param name="FileBytes">Bytes which represent the entire file</param>
        public static FileInfo SaveToFileSystem(string FilePath, byte[] FileBytes)
        {
            FileStream      objFileStream;
            ImpersonateUser iu = new ImpersonateUser();
            FileInfo        objFileInfo;

            bool bOverrideCredentials = false;

            if (ConfigurationManager.AppSettings["OverrideCredentialsForSaveToFileSystem"] == "1")
            {
                bOverrideCredentials = true;
            }

            try
            {
                if (bOverrideCredentials)
                {
                    //Impersonate user so we can write to file system (without giving write access to the website's user)
                    iu.Impersonate(ConfigurationManager.AppSettings["g_PublishDomain"], ConfigurationManager.AppSettings["g_PublishUser"], ConfigurationManager.AppSettings["g_PublishPwd"]);
                }

                objFileStream = new FileStream(FilePath, FileMode.Create);

                objFileStream.Write(FileBytes, 0, FileBytes.Length);

                objFileStream.Close();

                objFileInfo = new FileInfo(FilePath);
            }

            catch (Exception ex)
            {
                throw new ApplicationException("Error Saving File to File System <br />" + ex.Message + "<br />" + ex.InnerException);
            }
            finally
            {
                if (bOverrideCredentials)
                {
                    iu.Undo();
                }
            }

            return(objFileInfo);
        }
コード例 #4
0
        /// <summary>
        /// Method will save file to file system.  It will overwrite existing files at the same path.
        /// </summary>
        /// <param name="FilePath">Fully qualified File path including name and extension</param>
        /// <param name="FileBytes">Bytes which represent the entire file</param>
        public static FileInfo SaveToFileSystem(string FilePath, byte[] FileBytes)
        {
            FileStream objFileStream;
            ImpersonateUser iu = new ImpersonateUser();
            FileInfo objFileInfo;

            bool bOverrideCredentials = false;
            if (ConfigurationManager.AppSettings["OverrideCredentialsForSaveToFileSystem"] == "1")
            {
                bOverrideCredentials = true;
            }

            try
            {
                if (bOverrideCredentials)
                {
                    //Impersonate user so we can write to file system (without giving write access to the website's user)
                    iu.Impersonate(ConfigurationManager.AppSettings["g_PublishDomain"], ConfigurationManager.AppSettings["g_PublishUser"], ConfigurationManager.AppSettings["g_PublishPwd"]);
                }

                objFileStream = new FileStream(FilePath, FileMode.Create);

                objFileStream.Write(FileBytes, 0, FileBytes.Length);

                objFileStream.Close();

                objFileInfo = new FileInfo(FilePath);
            }

            catch (Exception ex)
            {
                throw new ApplicationException("Error Saving File to File System <br />" + ex.Message + "<br />" + ex.InnerException);
            }
            finally
            {
                if (bOverrideCredentials)
                {
                    iu.Undo();
                }
            }

            return objFileInfo;
        }