Exemplo n.º 1
0
 /// -----------------------------------------------------------------------------
 /// <summary>
 /// Tries to copy a file in the file system
 /// </summary>
 /// <param name="sourceFileName">The name of the source file</param>
 /// <param name="destFileName">The name of the destination file</param>
 /// -----------------------------------------------------------------------------
 public static void CopyFile(string sourceFileName, string destFileName)
 {
     if (File.Exists(destFileName))
     {
         File.SetAttributes(destFileName, FileAttributes.Normal);
     }
     File.Copy(sourceFileName, destFileName, true);
 }
Exemplo n.º 2
0
 /// -----------------------------------------------------------------------------
 /// <summary>
 /// Tries to delete a file from the file system
 /// </summary>
 /// <param name="fileName">The name of the file</param>
 /// -----------------------------------------------------------------------------
 public static void DeleteFile(string fileName)
 {
     fileName = FixPath(fileName);
     if (File.Exists(fileName))
     {
         File.SetAttributes(fileName, FileAttributes.Normal);
         File.Delete(fileName);
     }
 }
Exemplo n.º 3
0
        public static string DeleteFiles(Array arrPaths)
        {
            var strExceptions = string.Empty;

            for (var i = 0; i < arrPaths.Length; i++)
            {
                var strPath = (arrPaths.GetValue(i) ?? string.Empty).ToString();
                var pos     = strPath.IndexOf("'", StringComparison.Ordinal);
                if (pos != -1)
                {
                    // the (') represents a comment to the end of the line
                    strPath = strPath.Substring(0, pos);
                }

                strPath = FixPath(strPath).TrimStart('\\');
                if (!string.IsNullOrEmpty(strPath))
                {
                    strPath = Path.Combine(Globals.ApplicationMapPath, strPath);
                    if (strPath.EndsWith("\\") && Directory.Exists(strPath))
                    {
                        var directoryInfo   = new System.IO.DirectoryInfo(strPath);
                        var applicationPath = Globals.ApplicationMapPath + "\\";
                        if (directoryInfo.FullName.StartsWith(applicationPath, StringComparison.InvariantCultureIgnoreCase) &&
                            !directoryInfo.FullName.Equals(applicationPath, StringComparison.InvariantCultureIgnoreCase))
                        {
                            try
                            {
                                Globals.DeleteFolderRecursive(strPath);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                                strExceptions += $"Processing folder ({strPath}) Error: {ex.Message}{Environment.NewLine}";
                            }
                        }
                    }
                    else
                    {
                        if (File.Exists(strPath))
                        {
                            try
                            {
                                File.SetAttributes(strPath, FileAttributes.Normal);
                                File.Delete(strPath);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                                strExceptions += $"Processing file ({strPath}) Error: {ex.Message}{Environment.NewLine}";
                            }
                        }
                    }
                }
            }

            return(strExceptions);
        }
Exemplo n.º 4
0
 /// -----------------------------------------------------------------------------
 /// <summary>
 /// Trys to delete a file from the file system
 /// </summary>
 /// <param name="fileName">The name of the file</param>
 /// -----------------------------------------------------------------------------
 public static void DeleteFile(string fileName)
 {
     if (File.Exists(fileName))
     {
         fileName = fileName.Replace('/', '\\');
         File.SetAttributes(fileName, FileAttributes.Normal);
         File.Delete(fileName);
     }
 }
Exemplo n.º 5
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes file in areas with a high degree of concurrent file access (i.e. caching, logging)
        /// This solves file concurrency issues under heavy load.
        /// </summary>
        /// <param name="fileName">String.</param>
        /// <param name="waitInMilliseconds">Int16.</param>
        /// <param name="maxAttempts">Int16.</param>
        /// <returns>Boolean.</returns>
        /// <remarks>
        /// </remarks>
        /// -----------------------------------------------------------------------------
        public static bool DeleteFileWithWait(string fileName, short waitInMilliseconds, short maxAttempts)
        {
            fileName = FixPath(fileName);
            if (!File.Exists(fileName))
            {
                return(true);
            }

            bool fileDeleted = false;
            int  i           = 0;

            while (fileDeleted != true)
            {
                if (i > maxAttempts)
                {
                    break;
                }

                i = i + 1;
                try
                {
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    fileDeleted = true; // we don't care if it didn't exist...the operation didn't fail, that's what we care about
                }
                catch (Exception exc)
                {
                    Logger.Error(exc);
                    fileDeleted = false;
                }

                if (fileDeleted == false)
                {
                    Thread.Sleep(waitInMilliseconds);
                }
            }

            return(fileDeleted);
        }
Exemplo n.º 6
0
        public static string ReadFile(string filePath)
        {
            StreamReader reader      = null;
            string       fileContent = string.Empty;

            try
            {
                reader      = File.OpenText(filePath);
                fileContent = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
            return(fileContent);
        }
Exemplo n.º 7
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a File to a Zip File.
        /// </summary>
        /// -----------------------------------------------------------------------------
        public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string fileName, string folder)
        {
            FileStream fs = null;

            try
            {
                // Open File Stream
                fs = File.OpenRead(FixPath(filePath));

                // Read file into byte array buffer
                var buffer = new byte[fs.Length];

                var len = fs.Read(buffer, 0, buffer.Length);
                if (len != fs.Length)
                {
                    Logger.ErrorFormat(
                        "Reading from " + filePath + " didn't read all data in buffer. " +
                        "Requested to read {0} bytes, but was read {1} bytes", fs.Length, len);
                }

                // Create Zip Entry
                var entry = new ZipEntry(Path.Combine(folder, fileName));
                entry.DateTime = DateTime.Now;
                entry.Size     = fs.Length;
                fs.Close();

                // Compress file and add to Zip file
                ZipFile.PutNextEntry(entry);
                ZipFile.Write(buffer, 0, buffer.Length);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
Exemplo n.º 8
0
 public static void UnzipResources(ZipInputStream zipStream, string destPath)
 {
     try
     {
         var zipEntry = zipStream.GetNextEntry();
         while (zipEntry != null)
         {
             HtmlUtils.WriteKeepAlive();
             var localFileName = zipEntry.Name;
             var relativeDir   = Path.GetDirectoryName(zipEntry.Name);
             if (!string.IsNullOrEmpty(relativeDir) && (!Directory.Exists(Path.Combine(destPath, relativeDir))))
             {
                 Directory.Create(Path.Combine(destPath, relativeDir), true);
             }
             if (!zipEntry.IsDirectory && (!string.IsNullOrEmpty(localFileName)))
             {
                 var fileNamePath = FixPath(Path.Combine(destPath, localFileName));
                 try
                 {
                     if (File.Exists(fileNamePath))
                     {
                         File.SetAttributes(fileNamePath, FileAttributes.Normal);
                         File.Delete(fileNamePath);
                     }
                     FileStream objFileStream = null;
                     try
                     {
                         File.Create(fileNamePath);
                         objFileStream = File.Open(fileNamePath);
                         int intSize = 2048;
                         var arrData = new byte[2048];
                         intSize = zipStream.Read(arrData, 0, arrData.Length);
                         while (intSize > 0)
                         {
                             objFileStream.Write(arrData, 0, intSize);
                             intSize = zipStream.Read(arrData, 0, arrData.Length);
                         }
                     }
                     finally
                     {
                         if (objFileStream != null)
                         {
                             objFileStream.Close();
                             objFileStream.Dispose();
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     Logger.Error(ex);
                 }
             }
             zipEntry = zipStream.GetNextEntry();
         }
     }
     finally
     {
         if (zipStream != null)
         {
             zipStream.Close();
             zipStream.Dispose();
         }
     }
 }