示例#1
0
        public SourceAccess([NotNull] string fileName, bool isReading = true, [CanBeNull] string id = null,
                            [CanBeNull] string recipient = null, bool keepEnencyrpted = false)
        {
            FullPath        = fileName;
            Reading         = isReading;
            Identifier      = id ?? FileSystemUtils.GetShortDisplayFileName(fileName, 40);
            Recipient       = recipient ?? string.Empty;
            KeepEnencyrpted = keepEnencyrpted;
            LeaveOpen       = false;
            FileType        = FromExtension(fileName);
            switch (FileType)
            {
            case FileTypeEnum.Zip when !isReading:
                IdentifierInContainer = FileSystemUtils.GetFileName(fileName).ReplaceCaseInsensitive(".zip", "");
                break;

            // for PGP we need a password/ passphrase for Zip we might need one later
            case FileTypeEnum.Pgp when isReading:
                EncryptedPassphrase = FunctionalDI.GetEncryptedPassphraseForFile(fileName);
                break;
            }
            if (!isReading && KeepEnencyrpted)
            {
                // remove entension
                var split = FileSystemUtils.SplitPath(fileName);
                var fn    = Path.Combine(split.DirectoryName, split.FileNameWithoutExtension);
                m_OpenStream = GetOpenStreamFunc(fn, false);
            }
            else
            {
                m_OpenStream = GetOpenStreamFunc(fileName, isReading);
            }
        }
示例#2
0
        public static bool DeleteFileQuestion(this string fileName, bool ask)
        {
            if (!FileSystemUtils.FileExists(fileName))
            {
                return(true);
            }
            if (!ask)
            {
                try
                {
                    File.Delete(fileName);
                    return(true);
                }
                catch
                {
                    // ignored
                }
                return(false);
            }
            var res  = FileSystemUtils.SplitPath(fileName);
            var disp = res.FileName;

            if (_MessageBox.Show(null,
                                 $"The file {disp} does exist, do you want to remove it?", "File", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
retry:
                try
                {
                    File.Delete(fileName);
                    return(true);
                }
                catch (Exception ex)
                {
                    if (_MessageBox.Show(null,
                                         $"The file {disp} could not be deleted.\n{ex.ExceptionMessages()}", "File",
                                         MessageBoxButtons.RetryCancel, MessageBoxIcon.Question) == DialogResult.Retry)
                    {
                        goto retry;
                    }
                }
            }
            return(false);
        }
 private async void BtnOpenFile_ClickAsync(object sender, EventArgs e)
 {
     try
     {
         var split       = FileSystemUtils.SplitPath(textBoxFile.Text);
         var newFileName = WindowsAPICodePackWrapper.Open(
             split.DirectoryName,
             "Delimited File",
             "Delimited files (*.csv;*.txt;*.tab;*.tsv)|*.csv;*.txt;*.tab;*.tsv|All files (*.*)|*.*",
             split.FileName);
         if (!string.IsNullOrEmpty(newFileName))
         {
             await ChangeFileNameAsync(newFileName);
         }
     }
     catch (Exception ex)
     {
         this.ShowError(ex);
     }
 }