public TemporaryFileCloner(string wordDocumentFilePath)
        {
            FilePath =
                ZlpPathHelper.Combine(
                    Path.GetTempPath(),
                    $@"{Guid.NewGuid()}-{ZlpPathHelper.GetFileNameWithoutExtension(wordDocumentFilePath)}");

            ZlpSafeFileOperations.SafeCopyFile(
                wordDocumentFilePath,
                FilePath);
        }
        public TemporaryFileCloner(string wordDocumentFilePath)
        {
            _tempFilePath =
                ZlpPathHelper.Combine(
                    Path.GetTempPath(),
                    string.Format(@"{0}-{1}",
                                  Guid.NewGuid(),
                                  ZlpPathHelper.GetFileNameWithoutExtension(wordDocumentFilePath)));

            ZlpSafeFileOperations.SafeCopyFile(
                wordDocumentFilePath,
                _tempFilePath,
                true);
        }
 public static bool SafeExists(this ZlpFileOrDirectoryInfo i)
 {
     if (i == null || i.IsEmpty)
     {
         return(false);
     }
     else if (i.IsDirectory)
     {
         return(ZlpSafeFileOperations.SafeDirectoryExists(i.Directory));
     }
     else if (i.IsFile)
     {
         return(ZlpSafeFileOperations.SafeFileExists(i.File));
     }
     else
     {
         return(false);
     }
 }
        public static void ExportToExcelFile(
            DataSet dataSet,
            string filePath)
        {
            var p = new ExcelPackage(new ZlpFileInfo(filePath).ToBuiltIn());

            var wb        = p.Workbook;
            var loopCount = 0;

            while (wb.Worksheets.Count > 0 && loopCount++ < 100)
            {
                try
                {
                    wb.Worksheets.Delete(0);
                }
                catch (KeyNotFoundException)
                {
                    // Ignore.
                }

                try
                {
                    wb.Worksheets.Delete(1);
                }
                catch (KeyNotFoundException)
                {
                    // Ignore.
                }
            }

            foreach (DataTable table in dataSet.Tables)
            {
                var ws = wb.Worksheets.Add(table.TableName);
                processSheet(table, ws);
            }

            ZlpSafeFileOperations.SafeDeleteFile(filePath);
            p.Save();
        }
        private void backupFiles()
        {
            lock (_backupLock)
            {
                foreach (var resxFile in _resxFiles)
                {
                    var bak = resxFile.FilePath;

                    // Delete old bak files
                    if (ZlpIOHelper.FileExists(bak.FullName + @".bak"))
                    {
                        // Remove ReadOnly-attribute.
                        removeReadOnlyAttributes(
                            new ZlpFileInfo(bak.FullName + @".bak"));
                        ZlpSafeFileOperations.SafeDeleteFile(bak + @".bak");
                    }

                    ZlpSafeFileOperations.SafeCopyFile(
                        resxFile.FilePath.FullName,
                        bak.FullName + @".bak");
                }
            }
        }
Beispiel #6
0
 public static void SafeMove(this ZlpFileInfo sourcePath, ZlpFileInfo dstFilePath)
 {
     ZlpSafeFileOperations.SafeMoveFile(sourcePath, dstFilePath);
 }
        /// <summary>
        /// Store files to FileSystem
        /// </summary>
        private void storeFiles(Project project)
        {
            foreach (var resxFile in _resxFiles)
            {
                if (resxFile.FilePath.Exists)
                {
                    if ((ZlpIOHelper.GetFileAttributes(resxFile.FilePath.FullName) &
                         ZetaLongPaths.Native.FileAttributes.Readonly) != 0)
                    {
                        if (_gridEditableData?.Project != null)
                        {
                            switch (_gridEditableData.Project.ReadOnlyFileOverwriteBehaviour)
                            {
                            case ReadOnlyFileOverwriteBehaviour.Overwrite:
                                // Simply continue to code below.
                                break;

                            case ReadOnlyFileOverwriteBehaviour.Ask:
                                var h = CanOverwrite;
                                resxFile.FilePath.Refresh();
                                if (h != null)
                                {
                                    switch (h(resxFile.FilePath))
                                    {
                                    case AskOverwriteResult.Overwrite:
                                        // Simply continue to code below.
                                        break;

                                    case AskOverwriteResult.Skip:
                                        continue;

                                    case AskOverwriteResult.Fail:
                                        throw new Exception(
                                                  string.Format(
                                                      Resources
                                                      .SR_DataProcessing_storeFiles_Save_operation_was_cancelled_at_file,
                                                      resxFile.FilePath.Name));

                                    default:
                                        throw new ArgumentOutOfRangeException();
                                    }
                                }
                                break;

                            case ReadOnlyFileOverwriteBehaviour.Skip:
                                continue;

                            case ReadOnlyFileOverwriteBehaviour.Fail:
                                throw new Exception(
                                          string.Format(
                                              Resources
                                              .SR_DataProcessing_storeFiles_Saving_failed_because_of_read_only_file,
                                              resxFile.FilePath.Name));

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                    }

                    removeReadOnlyAttributes(resxFile.FilePath);
                    ZlpSafeFileOperations.SafeDeleteFile(resxFile.FilePath.FullName);
                }

                var settings =
                    new XmlWriterSettings
                {
                    Indent      = true,
                    IndentChars = project.ResXIndentChar,
                    Encoding    = Encoding.UTF8
                };

                ZlpSimpleFileAccessProtector.Protect(
                    delegate
                {
                    using (var sw = new StreamWriter(
                               resxFile.FilePath.FullName,
                               false,
                               new UTF8Encoding(true))) // https://github.com/UweKeim/ZetaResourceEditor/issues/24
                        using (var write = XmlWriter.Create(sw, settings))
                        {
                            var doc = resxFile.Document;
                            doc.Save(write);
                        }
                });
            }
        }
Beispiel #8
0
        private static void safeDeleteFile(
            string filePath)
        {
            Trace.TraceInformation(@"About to safe-delete file '{0}'.", filePath);

            if (!string.IsNullOrEmpty(filePath) &&
                safeFileExists(filePath))
            {
                try
                {
                    var attributes =
                        ZlpIOHelper.GetFileAttributes(filePath);

                    // Remove read-only attributes.
                    if ((attributes & FileAttributes.Readonly) != 0)
                    {
                        ZlpIOHelper.SetFileAttributes(
                            filePath,
                            attributes & (~(FileAttributes.Readonly)));
                    }

                    ZlpSafeFileOperations.SafeDeleteFile(filePath);
                }
                catch (UnauthorizedAccessException x)
                {
                    var newFilePath =
                        string.Format(
                            @"{0}.{1:N}.deleted",
                            filePath,
                            Guid.NewGuid());

                    Trace.TraceWarning(
                        string.Format(
                            @"Caught UnauthorizedAccessException while deleting file '{0}'. " +
                            @"Renaming now to '{1}'.",
                            filePath,
                            newFilePath),
                        x);

                    ZlpIOHelper.MoveFile(
                        filePath,
                        newFilePath);
                }
                catch (Exception x)
                {
                    var newFilePath =
                        string.Format(
                            @"{0}.{1:N}.deleted",
                            filePath,
                            Guid.NewGuid());

                    Trace.TraceWarning(
                        string.Format(
                            @"Caught IOException while deleting file '{0}'. " +
                            @"Renaming now to '{1}'.",
                            filePath,
                            newFilePath),
                        x);

                    ZlpIOHelper.MoveFile(
                        filePath,
                        newFilePath);
                }
            }
            else
            {
                Trace.TraceInformation(@"Not safe-deleting file '{0}', " +
                                       @"because the file does not exist.", filePath);
            }
        }
Beispiel #9
0
 public static ZlpFileInfo SafeMove(this ZlpFileInfo sourcePath, string dstFilePath)
 {
     ZlpSafeFileOperations.SafeMoveFile(sourcePath, dstFilePath);
     return(sourcePath);
 }
 private void doDispose()
 {
     ZlpSafeFileOperations.SafeDeleteFile(FilePath);
 }
Beispiel #11
0
 public static void SafeDeleteContents(this ZlpDirectoryInfo folderPath)
 {
     ZlpSafeFileOperations.SafeDeleteDirectoryContents(folderPath);
 }
Beispiel #12
0
 public static void SafeCopy(this ZlpFileInfo sourcePath, ZlpFileInfo dstFilePath, bool overwrite = true)
 {
     ZlpSafeFileOperations.SafeCopyFile(sourcePath, dstFilePath, overwrite);
 }
Beispiel #13
0
 public static ZlpFileInfo SafeDelete(this ZlpFileInfo filePath)
 {
     ZlpSafeFileOperations.SafeDeleteFile(filePath);
     return(filePath);
 }
Beispiel #14
0
 public static bool SafeExists(this ZlpFileInfo filePath)
 {
     return(ZlpSafeFileOperations.SafeFileExists(filePath));
 }
Beispiel #15
0
 public static bool SafeExists(this ZlpDirectoryInfo folderPath)
 {
     return(ZlpSafeFileOperations.SafeDirectoryExists(folderPath));
 }
Beispiel #16
0
 public static void SafeDelete(this ZlpFileInfo filePath)
 {
     ZlpSafeFileOperations.SafeDeleteFile(filePath);
 }
Beispiel #17
0
 public static ZlpFileInfo SafeCopy(this ZlpFileInfo sourcePath, string dstFilePath, bool overwrite = true)
 {
     ZlpSafeFileOperations.SafeCopyFile(sourcePath, dstFilePath, overwrite);
     return(sourcePath);
 }
Beispiel #18
0
 public static ZlpDirectoryInfo SafeDelete(this ZlpDirectoryInfo folderPath)
 {
     ZlpSafeFileOperations.SafeDeleteDirectory(folderPath);
     return(folderPath);
 }