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");
                }
            }
        }
        /// <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);
                        }
                });
            }
        }
Exemplo n.º 4
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);
            }
        }
 private void doDispose()
 {
     ZlpSafeFileOperations.SafeDeleteFile(FilePath);
 }
Exemplo n.º 6
0
 public static void SafeDelete(this ZlpFileInfo filePath)
 {
     ZlpSafeFileOperations.SafeDeleteFile(filePath);
 }
Exemplo n.º 7
0
 public static ZlpFileInfo SafeDelete(this ZlpFileInfo filePath)
 {
     ZlpSafeFileOperations.SafeDeleteFile(filePath);
     return(filePath);
 }