public static bool IsDirectoryWritable(string dirPath, bool throwIfFails = false) { try { using (FileStream fs = File.Create( Path.Combine( dirPath, Path.GetRandomFileName() ), 1, FileOptions.DeleteOnClose) ) { } return(true); } catch { if (throwIfFails) { throw; } else { return(false); } } }
private static bool DownloadFile(string sourceURL, string fileName) { try { if (File.Exists(fileName)) { return(true); } using (Stream stream = Misc.DownloadWebBinary(sourceURL)) { if (stream == null) { return(false); } string destinationFolder = Directory.GetParent(fileName).FullName; if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); } using (var fileStream = File.Create(fileName)) { CopyStream(stream, fileStream); } } return(true); } catch { return(false); } }
public void TestReplaceIgnoreMerge() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const bool ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, null, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); } }
public void TestDecrypt() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename, 200)) { } var preAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual((FileAttributes)0, (preAttrib & FileAttributes.Encrypted)); var fi = new FileInfo(tempLongPathFilename); fi.Encrypt(); var postAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual(FileAttributes.Encrypted, (postAttrib & FileAttributes.Encrypted)); fi.Decrypt(); postAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual((FileAttributes)0, (postAttrib & FileAttributes.Encrypted)); } finally { File.Delete(tempLongPathFilename); } }
/// <summary> /// Opens an file for writing /// </summary> /// <param name="path">The path.</param> /// <remarks> /// There could be one steps a) in case its to be uploaded a temp file will be created /// </remarks> public static ImprovedStream OpenWrite(string path, IProcessDisplay processDisplay = null, string recipient = null) { var retVal = new ImprovedStream() { WritePath = path.RemovePrefix(), ProcessDisplay = processDisplay, Recipient = recipient }; if (path.AssumePgp() || path.AssumeGZip()) { Log.Debug("Creating temporary file"); retVal.TempFile = Path.GetTempFileName(); // download the file to a temp file retVal.BaseStream = File.Create(retVal.TempFile); } else { FileSystemUtils.FileDelete(path.LongPathPrefix()); retVal.BaseStream = File.Create(path.LongPathPrefix()); } retVal.Stream = retVal.BaseStream; return(retVal); }
public Stream Create(string path) { var name = path.Split('\\').Last(); ValidateNameLength(name); return(File.Create(path)); }
public static String CreateNewEmptyFile(String longPathDirectory, String filename) { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(Path.DirectorySeparatorChar).Append(filename).ToString(); using (File.Create(tempLongPathFilename)) { } return(tempLongPathFilename); }
public static string CreateNewEmptyFile(string longPathDirectory) { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(Path.DirectorySeparatorChar).Append(Path.GetRandomFileName()).ToString(); using (File.Create(tempLongPathFilename)) { } return(tempLongPathFilename); }
public void TestReplaceIgnoreMerge() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { try { fileStream.WriteByte(42); } catch (Exception) { File.Delete(tempLongPathFilename); throw; } } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { try { fileStream.WriteByte(52); } catch (Exception) { File.Delete(tempLongPathFilename2); throw; } } var fi = new FileInfo(tempLongPathFilename); try { const bool ignoreMetadataErrors = true; var fi2 = fi.Replace(tempLongPathFilename2, null, ignoreMetadataErrors); Assert.IsNotNull(fi2); Assert.AreEqual(tempLongPathFilename2, fi2.FullName); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); } }
public void TestCreateWithBuffersizeFileOptions() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var s = File.Create(tempLongPathFilename, 200, FileOptions.DeleteOnClose)) { s.WriteByte(42); s.Seek(0, SeekOrigin.Begin); Assert.AreEqual(42, s.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); }
public void TestReplaceIgnoreMergeWithReadonlyBackupPath() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); var tempBackupPathName = new StringBuilder(longPathDirectory).Append(@"\readonly").ToString(); var di = new DirectoryInfo(tempBackupPathName); di.Create(); var attr = di.Attributes; di.Attributes = attr | FileAttributes.ReadOnly; var tempBackupLongPathFilename = new StringBuilder(tempBackupPathName).Append(@"\").Append("backup").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const bool ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempBackupLongPathFilename)); } finally { di.Attributes = attr; if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); File.Delete(tempBackupLongPathFilename); if (Directory.Exists(tempBackupPathName)) { Directory.Delete(tempBackupPathName); } } }
public void TestReadAllBytes() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } try { Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename))); } finally { File.Delete(tempLongPathFilename); } }
/// <summary> /// Transforms a file into its cryptographically strong equivalent using AES256 in CBC mode. /// </summary> /// <param name="originalFile">The path to the file to be cryptographically transformed.</param> /// <param name="encryptedFile">The path where the new file will be created.</param> /// <param name="password">The key to be used during the cryptographic transformation.</param> /// <returns>A flag determining whether or not the operation was successful.</returns> public static bool EncryptFile(string originalFile, string encryptedFile, byte[] password) { try { string originalFilename = originalFile; string encryptedFilename = encryptedFile; using (var originalStream = LongFile.Open(originalFile, FileMode.Open)) using (var encryptedStream = LongFile.Create(encryptedFile, 8192)) using (var encTransform = new EtM_EncryptTransform(key: password)) using (var crypoStream = new CryptoStream(encryptedStream, encTransform, CryptoStreamMode.Write)) { originalStream.CopyTo(crypoStream); } return(true); } catch { return(false); } }
public void TestCreate() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); s.Seek(0, SeekOrigin.Begin); Assert.AreEqual(42, s.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenWithAccess() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append(Path.GetRandomFileName()).ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (File.Open(tempLongPathFilename, FileMode.Open, FileAccess.Read)) { } } finally { File.Delete(tempLongPathFilename); } }
/// <summary> /// Asynchronously transforms a file into its cryptographically strong equivalent using AES256 in CBC mode. /// </summary> /// <param name="originalFile">The path to the file to be cryptographically transformed.</param> /// <param name="encryptedFile">The path where the new file will be created.</param> /// <param name="password">The key to be used during the cryptographic transformation.</param> /// <returns>A flag determining whether or not the operation was successful.</returns> public static async Task <bool> EncryptFileAsync(string originalFile, string encryptedFile, string password) { try { string originalFilename = originalFile; string encryptedFilename = encryptedFile; using (var originalStream = LongFile.Open(originalFile, FileMode.Open)) using (var encryptedStream = LongFile.Create(encryptedFile, 8192)) using (var encTransform = new EtM_EncryptTransform(key: password.ToBytes())) using (var crypoStream = new CryptoStream(encryptedStream, encTransform, CryptoStreamMode.Write)) { await originalStream.CopyToAsync(crypoStream); return(true); } } catch { return(false); } }
public void TestOpenRead() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (var stream = File.OpenRead(tempLongPathFilename)) { Assert.AreEqual(42, stream.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenExisting() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (var stream = File.Open(tempLongPathFilename, FileMode.Open)) { Assert.IsNotNull(stream); } } finally { File.Delete(tempLongPathFilename); } }
public void TestReadAllBytesOnHugeFile() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.Seek(((long)int.MaxValue) + 1, SeekOrigin.Begin); fileStream.WriteByte(42); } Assert.Throws <IOException>(() => { try { Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename))); } finally { File.Delete(tempLongPathFilename); } }); }
public void TestReplaceIgnoreMergeWithInvalidBackupPath() { Assert.Throws <DirectoryNotFoundException>(() => { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); var tempBackupLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\gibberish\").Append("backup").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const bool ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempBackupLongPathFilename)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); File.Delete(tempBackupLongPathFilename); } }); }
/// <summary> /// Asynchronously transforms a cryptographic file into its original form using AES256 in CBC mode. /// </summary> /// <param name="originalFile">The path to the file to be transformed into its decrypted form.</param> /// <param name="decryptedFile">The location the decrypted file will be written to.</param> /// <param name="password">The key to use during the cryptographic transformation.</param> /// <returns>A flag determining whether or not the operation was successful or not.</returns> public static async Task <bool> DecryptFileAsync(string originalFile, string decryptedFile, byte[] password) { try { string originalFilename = originalFile; string decryptedFilename = decryptedFile; using (var encryptedStream = LongFile.Open(originalFilename, FileMode.Open)) using (var decryptedStream = LongFile.Create(decryptedFilename, 8192)) using (var decTransform = new EtM_DecryptTransform(key: password)) { using (var cryptoStream = new CryptoStream(encryptedStream, decTransform, CryptoStreamMode.Read)) await cryptoStream.CopyToAsync(decryptedStream); if (!decTransform.IsComplete) { throw new Exception("Not all blocks have been decrypted."); } } return(true); } catch { return(false); } }
private void CloseWrite() { // If we are writing we have possibly two steps, // a) compress / encrypt the data from temp // b) upload the data to sFTP if (ProcessDisplay == null) { ProcessDisplay = new DummyProcessDisplay(); } if (WritePath.AssumeGZip() || WritePath.AssumePgp()) { try { // Compress the file if (WritePath.AssumeGZip()) { Log.Debug("Compressing temporary file to GZip file"); using (var inFile = File.OpenRead(TempFile)) { // Create the compressed file. using (var outFile = File.Create(WritePath)) { using (var compress = new System.IO.Compression.GZipStream(outFile, System.IO.Compression.CompressionMode.Compress)) { var processDispayTime = ProcessDisplay as IProcessDisplayTime; var inputBuffer = new byte[16384]; var max = (int)(inFile.Length / inputBuffer.Length); ProcessDisplay.Maximum = max; var count = 0; int length; while ((length = inFile.Read(inputBuffer, 0, inputBuffer.Length)) > 0) { compress.Write(inputBuffer, 0, length); ProcessDisplay.CancellationToken.ThrowIfCancellationRequested(); if (processDispayTime != null) { ProcessDisplay.SetProcess( $"GZip {processDispayTime.TimeToCompletion.PercentDisplay}{processDispayTime.TimeToCompletion.EstimatedTimeRemainingDisplaySeperator}", count); } else { ProcessDisplay.SetProcess($"GZip {count:N0}/{max:N0}", count); } count++; } } } } } // need to encrypt the file else if (WritePath.AssumePgp()) { using (FileStream inputStream = new FileInfo(TempFile).OpenRead(), output = new FileStream(WritePath.LongPathPrefix(), FileMode.Create)) { Log.Debug("Encrypting temporary file to PGP file"); ApplicationSetting.ToolSetting.PGPInformation.PgpEncrypt(inputStream, output, Recipient, ProcessDisplay); } } } finally { Log.Debug("Removing temporary file"); File.Delete(TempFile); } } }
public static FileStream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity) { return(File.Create(path, bufferSize, options, fileSecurity)); }
public static FileStream Create(string path, int bufferSize) { return(File.Create(path, bufferSize)); }
public static FileStream Create(string path) { return(File.Create(path)); }