private void encryptFileToolStripMenuItem_Click(object sender, EventArgs e) { FileInfo fi = new FileInfo(this.openFileDialog1.FileName); if(!fi.Exists) { MessageBox.Show("File Not Found"); return; } if(fi.Attributes.ToString().Contains(FileAttributes.Encrypted.ToString())) { MessageBox.Show("File is already encrypted"); return; } if(MessageBox.Show("Are you sure you want to encrypt this file?\n\nOnly this user will be able to decrypt","File Peeker",MessageBoxButtons.YesNoCancel) == DialogResult.Yes) { fi.IsReadOnly = false; try { fi.Encrypt(); fi.Refresh(); getFileInfo(fi); }catch(Exception ex) { MessageBox.Show(ex.Message); } } }
private bool HandleFile(FileInfo file) { UpdateStatistics(); // Ignore EFS temporary files, etc. if (Regex.IsMatch(file.Name, @"efs\d{1,}.tmp", RegexOptions.IgnoreCase) || file.Name.ToLower() == "thumbs.db" || file.Name.ToLower() == "desktop.ini" || file.Attributes.HasFlag(FileAttributes.System)) { _ignored++; return false; } // Skip files that are already encrypted if (file.Attributes.HasFlag(FileAttributes.Encrypted)) { _skipped++; return true; } _file = file.FullName; try { // Here we're seeing if the file is set ReadOnly and resetting that status // after we encrypt it (since ReadOnly files are not encryptable) var readOnly = file.Attributes.HasFlag(FileAttributes.ReadOnly); if (readOnly) { file.Attributes = file.Attributes.Remove(FileAttributes.ReadOnly); } if (!DRY_RUN) { file.Encrypt(); } if (readOnly) { file.Attributes = file.Attributes.Add(FileAttributes.ReadOnly); } } catch (IOException e) { LogEncryptionException(file, e); return false; } catch (UnauthorizedAccessException e) { LogEncryptionException(file, e); return false; } catch (ArgumentException e) { LogEncryptionException(file, e); return false; } _encrypted++; return true; }
private void decryptFileToolStripMenuItem_Click(object sender, EventArgs e) { FileInfo fi = new FileInfo(this.openFileDialog1.FileName); if(!fi.Exists) { MessageBox.Show("File Not Found"); return; } if(!fi.Attributes.ToString().Contains(FileAttributes.Encrypted.ToString())) { MessageBox.Show("File is not encrypted"); return; } fi.IsReadOnly = false; try { fi.Encrypt(); fi.Refresh(); getFileInfo(fi); }catch(Exception ex) { MessageBox.Show(ex.Message); } }