public static string ShortFileName(this string longPath)
        {
            if (string.IsNullOrEmpty(longPath))
            {
                return(longPath);
            }

            var fi = new FileInfo(longPath);

            uint bufferSize      = 512;
            var  shortNameBuffer = new StringBuilder((int)bufferSize);

            // we might be asked to build a short path when the file does not exist yet, this would fail
            if (fi.Exists)
            {
                var length = GetShortPathName(longPath, shortNameBuffer, bufferSize);
                if (length > 0)
                {
                    return(shortNameBuffer.ToString().RemovePrefix());
                }
            }

            // if we have at least the directory shorten this...
            if (fi.Directory.Exists)
            {
                var length = GetShortPathName(fi.Directory.FullName, shortNameBuffer, bufferSize);
                if (length > 0)
                {
                    return((shortNameBuffer.ToString() + "\\" + fi.Name).RemovePrefix());
                }
            }

            throw new ApplicationException($"Could not get a short path for the file ${longPath}");
        }
        internal async Task <FileSystemResult <IFile> > InternalCreateFile(DirectoryImplementation dir, string name, Stream readstream, CancellationToken token, IProgress <FileProgress> progress, Dictionary <string, object> properties)
        {
            if (properties == null)
            {
                properties = new Dictionary <string, object>();
            }
            string path = name;

            if (dir.Parent != null)
            {
                path = Path.Combine(dir.FullName, path);
            }
            Stream s = File.Open(path, FileMode.Create, FileAccess.Write);

            byte[] block = new byte[1024 * 128];
            long   left  = readstream.Length;

            do
            {
                int size  = (int)Math.Min(left, block.Length);
                int rsize = await readstream.ReadAsync(block, 0, size, token);

                await s.WriteAsync(block, 0, rsize, token);

                left -= rsize;
                FileProgress p = new FileProgress
                {
                    Percentage   = ((float)(readstream.Length - left) * 100) / readstream.Length,
                    TotalSize    = readstream.Length,
                    TransferSize = readstream.Length - left
                };
                progress.Report(p);
            } while (left > 0 && !token.IsCancellationRequested);
            s.Close();
            if (token.IsCancellationRequested)
            {
                try
                {
                    File.Delete(path);
                }
                catch
                {
                    // ignored
                }
                return(new FileSystemResult <IFile>("Transfer canceled"));
            }
            FileInfo finfo = new FileInfo(path);

            if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)))
            {
                finfo.LastWriteTime = (DateTime)properties.First(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value;
            }
            if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)))
            {
                finfo.CreationTime = (DateTime)properties.First(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value;
            }
            LocalFile f = new LocalFile(finfo, FS);

            return(new FileSystemResult <IFile>(f));
        }
Exemple #3
0
        public void TestAppendText()
        {
            var filename = new StringBuilder(longPathDirectory).Append(@"\").Append("file16.ext").ToString();

            using (var writer = File.CreateText(filename))
            {
                writer.Write("start");
            }
            Assert.IsTrue(File.Exists(filename));

            try
            {
                using (var writer = new FileInfo(filename).AppendText())
                {
                    writer.WriteLine("end");
                }

                using (var reader = File.OpenText(filename))
                {
                    var text = reader.ReadLine();
                    Assert.AreEqual("startend", text);
                }
            }
            finally
            {
                File.Delete(filename);
            }
        }
Exemple #4
0
        public void TestDisplayPath()
        {
            var sfi = new System.IO.FileInfo(@"c:\Windows\notepad.exe");
            var fi  = new FileInfo(@"c:\Windows\notepad.exe");

            Assert.AreEqual(sfi.ToString(), fi.DisplayPath);
        }
Exemple #5
0
        public void TestOpenAppend()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file26.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            using (var streamWriter = fi.CreateText())
            {
                streamWriter.WriteLine("file26");
            }
            try
            {
                using (var fileStream = fi.Open(System.IO.FileMode.Append))
                {
                    Assert.IsNotNull(fileStream);
                    using (var streamWriter = new System.IO.StreamWriter(fileStream))
                    {
                        streamWriter.WriteLine("eof");
                    }
                }

                Assert.AreEqual("file26" + Environment.NewLine + "eof" + Environment.NewLine, File.ReadAllText(fi.FullName));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #6
0
        public void FileInfoReturnsCorrectDirectoryNameForLongPathFile()
        {
            Assert.IsTrue(Directory.Exists(longPathDirectory));
            string tempLongPathFilename;

            do
            {
                tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));
            Assert.IsFalse(File.Exists(tempLongPathFilename));

            using (var writer = File.CreateText(tempLongPathFilename))
            {
                writer.WriteLine("test");
            }
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(longPathDirectory, fileInfo.DirectoryName);
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #7
0
        public void TestMoveTo()
        {
            var tempLongPathFilename     = new StringBuilder(longPathDirectory).Append(@"\").Append("file21.ext").ToString();
            var tempDestLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file21-1.ext").ToString();

            Assert.IsFalse(File.Exists(tempLongPathFilename));
            File.Copy(longPathFilename, tempLongPathFilename);
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));

                var fi = new FileInfo(tempLongPathFilename);
                fi.MoveTo(tempDestLongPathFilename);

                try
                {
                    Assert.IsFalse(File.Exists(tempLongPathFilename));
                    Assert.IsTrue(File.Exists(tempDestLongPathFilename));
                }
                finally
                {
                    File.Delete(tempDestLongPathFilename);
                }
            }
            finally
            {
                if (File.Exists(tempLongPathFilename))
                {
                    File.Delete(tempLongPathFilename);
                }
            }
        }
Exemple #8
0
        public void TestOpenHidden()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file25.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            using (fi.Create())
            {
            }
            try
            {
                Assert.Throws <UnauthorizedAccessException>(() =>
                {
                    File.SetAttributes(fi.FullName, File.GetAttributes(fi.FullName) | FileAttributes.Hidden);

                    using (var fileStream = fi.Open(FileMode.Create))
                    {
                        Assert.IsNotNull(fileStream);
                    }
                });
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #9
0
        public override async Task <FileSystemResult> CopyAsync(IDirectory destination)
        {
            try
            {
                DirectoryImplementation to = destination as DirectoryImplementation;
                if (to == null)
                {
                    return(new FileSystemResult("Destination should be a Local Directory"));
                }
                if (to is LocalRoot)
                {
                    return(new FileSystemResult("Root cannot be destination"));
                }
                string destname = Path.Combine(to.FullName, Name);

                File.Copy(FullName, destname);
                FileInfo  finfo = new FileInfo(destname);
                LocalFile local = new LocalFile(finfo, FS);
                local.Parent = destination;
                to.IntFiles.Add(local);
                return(await Task.FromResult(new FileSystemResult()));
            }
            catch (Exception e)
            {
                return(new FileSystemResult(e.Message));
            }
        }
Exemple #10
0
        public void CanCreateFileInfoWithLongPathFile()
        {
            string tempLongPathFilename;

            do
            {
                tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));
            Assert.IsFalse(File.Exists(tempLongPathFilename));

            using (var writer = File.CreateText(tempLongPathFilename))
            {
                writer.WriteLine("test");
            }
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.IsNotNull(fileInfo);                 // just to use fileInfo variable
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #11
0
        public void TestCreateTextAndWrite()
        {
            Assert.IsTrue(Directory.Exists(longPathDirectory));
            string tempLongPathFilename;

            do
            {
                tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));
            Assert.IsFalse(File.Exists(tempLongPathFilename));

            const string fileText = "test";

            using (var writer = File.CreateText(tempLongPathFilename))
            {
                writer.WriteLine(fileText);
            }
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(fileText.Length + Environment.NewLine.Length, fileInfo.Length);
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #12
0
        public void TestGetAccessControlSections()
        {
            var filename = Util.CreateNewFile(longPathDirectory);

            try
            {
                var          fi       = new FileInfo(filename);
                FileSecurity security = fi.GetAccessControl(AccessControlSections.Access);
                Assert.IsNotNull(security);
                Assert.AreEqual(typeof(FileSystemRights), security.AccessRightType);
                Assert.AreEqual(typeof(FileSystemAccessRule), security.AccessRuleType);
                Assert.AreEqual(typeof(FileSystemAuditRule), security.AuditRuleType);
                Assert.IsTrue(security.AreAccessRulesCanonical);
                Assert.IsTrue(security.AreAuditRulesCanonical);
                Assert.IsFalse(security.AreAccessRulesProtected);
                Assert.IsFalse(security.AreAuditRulesProtected);
                var securityGetAccessRules = security.GetAuditRules(true, true, typeof(System.Security.Principal.NTAccount)).Cast <FileSystemAccessRule>();
                Assert.AreEqual(0, securityGetAccessRules.Count());
                AuthorizationRuleCollection perm = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                var ntAccount             = new System.Security.Principal.NTAccount(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                FileSystemAccessRule rule = perm.Cast <FileSystemAccessRule>().SingleOrDefault(e => ntAccount == e.IdentityReference);
                Assert.IsNotNull(rule);
                Assert.IsTrue((rule.FileSystemRights & FileSystemRights.FullControl) != 0);
            }
            finally
            {
                File.Delete(filename);
            }
        }
Exemple #13
0
 public override async Task <FileSystemResult> MoveAsync(IDirectory destination)
 {
     try
     {
         DirectoryImplementation to = destination as DirectoryImplementation;
         if (to == null)
         {
             return(new FileSystemResult("Destination should be a Local Directory"));
         }
         if (to is LocalRoot)
         {
             return(new FileSystemResult("Root cannot be destination"));
         }
         string destname = Path.Combine(to.FullName, Name);
         File.Move(FullName, destname);
         ((DirectoryImplementation)Parent).IntFiles.Remove(this);
         to.IntFiles.Add(this);
         this.Parent = to;
         file        = new FileInfo(destname);
         return(await Task.FromResult(new FileSystemResult()));
     }
     catch (Exception e)
     {
         return(new FileSystemResult(e.Message));
     }
 }
Exemple #14
0
        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);
            }
        }
Exemple #15
0
        public void TestSetLastWriteTimeMissingFile()
        {
            var      filename = Path.Combine(uncDirectory, "gibberish.ext");
            DateTime dateTime = DateTime.Now.AddDays(1);
            var      fi       = new FileInfo(filename);

            fi.LastWriteTime = dateTime;
        }
Exemple #16
0
        public void TestSetLastAccessTimeUtcMissingFile()
        {
            var      filename = Path.Combine(longPathDirectory, "gibberish.ext");
            DateTime dateTime = DateTime.UtcNow.AddDays(1);
            var      fi       = new FileInfo(filename);

            Assert.Throws <FileNotFoundException>(() => fi.LastAccessTimeUtc = dateTime);
        }
Exemple #17
0
        public void TestSetLastAccessTimeUtcMissingFile()
        {
            var      filename = Path.Combine(longPathDirectory, "gibberish.ext");
            DateTime dateTime = DateTime.UtcNow.AddDays(1);
            var      fi       = new FileInfo(filename);

            fi.LastAccessTimeUtc = dateTime;
        }
Exemple #18
0
        public void TestSetCreationTimeMissingFile()
        {
            var      filename = Path.Combine(longPathDirectory, "gibberish.ext");
            DateTime dateTime = DateTime.Now.AddDays(1);
            var      fi       = new FileInfo(filename);

            fi.CreationTime = dateTime;
        }
Exemple #19
0
        public void TestSetCreationTimeMissingFile()
        {
            var      filename = Path.Combine(uncDirectory, "gibberish.ext");
            DateTime dateTime = DateTime.Now.AddDays(1);
            var      fi       = new FileInfo(filename);

            Assert.Throws <FileNotFoundException>(() => fi.CreationTime = dateTime);
        }
Exemple #20
0
        public void TestOpenTextReadsExistingData()
        {
            var fi = new FileInfo(longPathFilename);

            using (var streamReader = fi.OpenText())
            {
                Assert.AreEqual("test", streamReader.ReadLine());
            }
        }
Exemple #21
0
        public void TestOpenReadReadsExistingData()
        {
            var fi = new FileInfo(longPathFilename);

            using (var fileStream = fi.OpenRead())
            {
                Assert.AreEqual('t', fileStream.ReadByte());
            }
        }
Exemple #22
0
        public void TestOpenOpen()
        {
            var fi = new FileInfo(longPathFilename);

            using (var fileStream = fi.Open(FileMode.Open))
            {
                Assert.IsNotNull(fileStream);
            }
        }
Exemple #23
0
        public void TestOpenCreateNew()
        {
            var fi = new FileInfo(filePath);

            using (var fileStream = fi.Open(FileMode.CreateNew))
            {
                Assert.IsNotNull(fileStream);
            }
        }
        /// <summary>
        /// Creates a new <see cref="FileSelectedEventArgs" />.
        /// </summary>
        /// <param name="routedEvent"></param>
        /// <param name="source">The source object</param>
        /// <param name="file">The full filename of the selected file</param>
        public FileSelectedEventArgs(RoutedEvent routedEvent, object source, string file)
            : base(routedEvent, source)
        {
            File = file;

            if (!string.IsNullOrWhiteSpace(file))
            {
                FileInfo = new FileInfo(file);
            }
        }
Exemple #25
0
        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);
            }
        }
Exemple #26
0
        public void TestOpenCreateNew()
        {
            var fi = new FileInfo(longPathFilename);

            Assert.Throws <IOException>(() =>
            {
                using (var fileStream = fi.Open(FileMode.CreateNew))
                {
                    Assert.IsNotNull(fileStream);
                }
            });
        }
 /// <summary>
 /// Creates a new <see cref="FileSystemController" />.
 /// </summary>
 public FileSystemController()
 {
     m_currentDirectory          = null;
     m_currentFile               = null;
     m_currentFileFullName       = null;
     m_currentDirectoryPathParts = null;
     m_directories               = null;
     m_files = null;
     m_showHiddenFilesAndDirectories = false;
     m_showSystemFilesAndDirectories = false;
     m_fileFilters       = null;
     m_fileFilterToApply = null;
 }
Exemple #28
0
        public void TestLengthWithBadPath()
        {
            var filename = new StringBuilder(uncDirectory).Append(Path.DirectorySeparatorChar).Append(Path.GetRandomFileName()).ToString();

            Pri.LongPath.FileInfo fi;
            long length;

            Assert.Throws <FileNotFoundException>(() =>
            {
                fi     = new FileInfo(filename);
                length = fi.Length;
            });
        }
 public void Delete(string sourcePath)
 {
     if (IsFolder(sourcePath))
     {
         Pri.LongPath.DirectoryInfo directoryInfo = new Pri.LongPath.DirectoryInfo(sourcePath);
         directoryInfo.Delete(true);
     }
     else
     {
         Pri.LongPath.FileInfo fileInfo = new Pri.LongPath.FileInfo(sourcePath);
         fileInfo.Delete();
     }
 }
Exemple #30
0
 /// <summary>
 /// Test if path is a symbolic link
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static bool IsSymbolicFile(string path)
 {
     if (path.Length < MaxFilePathLength)
     {
         var pathInfo = new FileInfo(path);
         return(pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint));
     }
     else
     {
         var pathInfo = new Pri.LongPath.FileInfo(path);
         return(pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint));
     }
 }
		public void CanCreateFileInfoWithLongPathFile()
		{
			string tempLongPathFilename;
			do
			{
				tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
			} while (File.Exists(tempLongPathFilename));
			Assert.IsFalse(File.Exists(tempLongPathFilename));

			using (var writer = File.CreateText(tempLongPathFilename))
			{
				writer.WriteLine("test");
			}
			try
			{
				Assert.IsTrue(File.Exists(tempLongPathFilename));
				var fileInfo = new FileInfo(tempLongPathFilename);
				Assert.IsNotNull(fileInfo); // just to use fileInfo variable
			}
			finally
			{
				File.Delete(tempLongPathFilename);
			}
		}
Exemple #32
0
        public void TestCreateTextAndWrite()
        {
            Assert.IsTrue(Directory.Exists(longPathDirectory));
            string tempLongPathFilename;
            do
            {
                tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));
            Assert.IsFalse(File.Exists(tempLongPathFilename));

            const string fileText = "test";
            using (var writer = File.CreateText(tempLongPathFilename))
            {
                writer.WriteLine(fileText);
            }
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(fileText.Length + Environment.NewLine.Length, fileInfo.Length);
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #33
0
 public void TestOpenTextReadsExistingData()
 {
     var fi = new FileInfo(longPathFilename);
     using (var streamReader = fi.OpenText())
     {
         Assert.AreEqual("test", streamReader.ReadLine());
     }
 }
Exemple #34
0
        public void FileInfoReturnsCorrectDirectoryForLongPathFile()
        {
            Assert.IsTrue(Directory.Exists(longPathDirectory));
            string tempLongPathFilename;
            do
            {
                tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));
            Assert.IsFalse(File.Exists(tempLongPathFilename));

            using (var writer = File.CreateText(tempLongPathFilename))
            {
                writer.WriteLine("test");
            }
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(longPathDirectory, fileInfo.Directory.FullName);
                Assert.AreEqual(Path.GetFileName(longPathDirectory), fileInfo.Directory.Name);
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #35
0
 public void TestDirectoryWithRoot()
 {
     var fi = new FileInfo(@"C:\");
     Assert.IsNull(fi.Directory);
 }
Exemple #36
0
 public void TestSetIsReadOnly()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     var fi = new FileInfo(filename);
     try
     {
         fi.IsReadOnly = true;
         Assert.IsTrue(fi.IsReadOnly);
     }
     finally
     {
         fi.IsReadOnly = false;
         fi.Delete();
     }
 }
Exemple #37
0
 public void TestSetLastWriteTimeUtcMissingFile()
 {
     var filename = Path.Combine(longPathDirectory, "gibberish.ext");
     DateTime dateTime = DateTime.UtcNow.AddDays(1);
     var fi = new FileInfo(filename);
     fi.LastWriteTimeUtc = dateTime;
 }
Exemple #38
0
 public void TestLengthWithBadPath()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     Pri.LongPath.FileInfo fi;
     try
     {
         fi = new FileInfo(filename);
     }
     catch
     {
         File.Delete(filename);
         throw;
     }
     fi.Delete();
     fi.Refresh();
     var l = fi.Length;
 }
Exemple #39
0
 public void TestSetAccessControl()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     try
     {
         var fi = new FileInfo(filename);
         var security = new FileSecurity();
         fi.SetAccessControl(security);
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #40
0
 public void TestGetIsReadOnly()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     try
     {
         var fi = new FileInfo(filename);
         Assert.IsTrue(fi.Exists);
         Assert.IsFalse(fi.IsReadOnly);
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #41
0
 public void TestLastWriteTime()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     try
     {
         var dateTime = DateTime.Now.AddDays(1);
         {
             var fiTemp = new FileInfo(filename) { LastWriteTime = dateTime };
         }
         var fi = new FileInfo(filename);
         Assert.AreEqual(dateTime, fi.LastWriteTime);
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #42
0
 public void TestGetAccessControlSections()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     try
     {
         var fi = new FileInfo(filename);
         FileSecurity security = fi.GetAccessControl(AccessControlSections.Access);
         Assert.IsNotNull(security);
         Assert.AreEqual(typeof(FileSystemRights), security.AccessRightType);
         Assert.AreEqual(typeof(FileSystemAccessRule), security.AccessRuleType);
         Assert.AreEqual(typeof(FileSystemAuditRule), security.AuditRuleType);
         Assert.IsTrue(security.AreAccessRulesCanonical);
         Assert.IsTrue(security.AreAuditRulesCanonical);
         Assert.IsFalse(security.AreAccessRulesProtected);
         Assert.IsFalse(security.AreAuditRulesProtected);
         var securityGetAccessRules = security.GetAuditRules(true, true, typeof(System.Security.Principal.NTAccount)).Cast<FileSystemAccessRule>();
         Assert.AreEqual(0, securityGetAccessRules.Count());
         AuthorizationRuleCollection perm = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
         var ntAccount = new System.Security.Principal.NTAccount(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
         FileSystemAccessRule rule = perm.Cast<FileSystemAccessRule>().SingleOrDefault(e => ntAccount == e.IdentityReference);
         Assert.IsNotNull(rule);
         Assert.IsTrue((rule.FileSystemRights & FileSystemRights.FullControl) != 0);
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #43
0
        public void TestDisplayPath()
        {
            var sfi = new System.IO.FileInfo(@"c:\Windows\notepad.exe");
            var fi = new FileInfo(@"c:\Windows\notepad.exe");

            Assert.AreEqual(sfi.ToString(), fi.DisplayPath);
        }
Exemple #44
0
        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);
            }
        }
Exemple #45
0
 public void TestOpenWriteWritesCorrectly()
 {
     var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file31a.ext").ToString();
     var fi = new FileInfo(tempLongPathFilename);
     try
     {
         using (var fileStream = fi.OpenWrite())
         {
             fileStream.WriteByte(42);
         }
         using (var fileStream = fi.OpenRead())
         {
             Assert.AreEqual(42, fileStream.ReadByte());
         }
     }
     finally
     {
         File.Delete(tempLongPathFilename);
     }
 }
Exemple #46
0
        public void TestMoveTo()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file21.ext").ToString();
            var tempDestLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file21-1.ext").ToString();
            Assert.IsFalse(File.Exists(tempLongPathFilename));
            File.Copy(longPathFilename, tempLongPathFilename);
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));

                var fi = new FileInfo(tempLongPathFilename);
                fi.MoveTo(tempDestLongPathFilename);

                try
                {
                    Assert.IsFalse(File.Exists(tempLongPathFilename));
                    Assert.IsTrue(File.Exists(tempDestLongPathFilename));
                }
                finally
                {
                    File.Delete(tempDestLongPathFilename);
                }
            }
            finally
            {
                if (File.Exists(tempLongPathFilename))
                    File.Delete(tempLongPathFilename);
            }
        }
Exemple #47
0
        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);
            }
        }
Exemple #48
0
        public void TestOpenCreatesEmpty()
        {
            var tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            try
            {
                using (var writer = File.CreateText(tempLongPathFilename))
                {
                    writer.WriteLine("test");
                }

                var fi = new FileInfo(tempLongPathFilename);
                using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read, FileShare.Read))
                {
                    Assert.AreEqual(-1, fileStream.ReadByte());
                }

            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #49
0
 public void TestSetCreationTimeMissingFile()
 {
     var filename = Path.Combine(longPathDirectory, "gibberish.ext");
     DateTime dateTime = DateTime.Now.AddDays(1);
     var fi = new FileInfo(filename);
     fi.CreationTime = dateTime;
 }
Exemple #50
0
        public void TestOpenHidden()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file25.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            using (fi.Create())
            {
            }
            try
            {
                File.SetAttributes(fi.FullName, File.GetAttributes(fi.FullName) | FileAttributes.Hidden);

                using (var fileStream = fi.Open(FileMode.Create))
                {
                    Assert.IsNotNull(fileStream);
                }

            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #51
0
 public void TestSetLastAccessTime()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     try
     {
         DateTime dateTime = DateTime.Now.AddDays(1);
         var fi = new FileInfo(filename);
         fi.LastAccessTime = dateTime;
         Assert.AreEqual(dateTime, File.GetLastAccessTime(filename));
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #52
0
 public void TestOpenOpen()
 {
     var fi = new FileInfo(longPathFilename);
     using (var fileStream = fi.Open(FileMode.Open))
     {
         Assert.IsNotNull(fileStream);
     }
 }
Exemple #53
0
        public void TestToString()
        {
            var fi = new FileInfo(longPathFilename);

            Assert.AreEqual(fi.DisplayPath, fi.ToString());
        }
Exemple #54
0
 public void TestOpenReadReadsExistingData()
 {
     var fi = new FileInfo(longPathFilename);
     using (var fileStream = fi.OpenRead())
     {
         Assert.AreEqual('t', fileStream.ReadByte());
     }
 }
Exemple #55
0
        public void TestSetLastWriteTimeUtc()
        {
            var filename = Util.CreateNewFile(longPathDirectory);
            try
            {
                DateTime dateTime = DateTime.UtcNow.AddDays(1);
                File.SetLastWriteTimeUtc(filename, dateTime);
                var fi = new FileInfo(filename);
                Assert.AreEqual(fi.LastWriteTimeUtc, dateTime);

            }
            finally
            {
                File.Delete(filename);
            }
        }
Exemple #56
0
 public void TestOpenReadWithWrite()
 {
     var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file31.ext").ToString();
     var fi = new FileInfo(tempLongPathFilename);
     try
     {
         using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read))
         {
             fileStream.WriteByte(43);
         }
     }
     finally
     {
         File.Delete(tempLongPathFilename);
     }
 }
Exemple #57
0
 public void TestGetLastWriteTimeUtc()
 {
     var filename = Util.CreateNewFile(longPathDirectory);
     try
     {
         var dateTime = File.GetLastWriteTimeUtc(filename);
         var fi = new FileInfo(filename);
         Assert.AreEqual(fi.LastWriteTimeUtc, dateTime);
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #58
0
        public void TestOpenAppend()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file26.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);
            using (var streamWriter = fi.CreateText())
            {
                streamWriter.WriteLine("file26");
            }
            try
            {
                using (var fileStream = fi.Open(System.IO.FileMode.Append))
                {
                    Assert.IsNotNull(fileStream);
                    using (var streamWriter = new System.IO.StreamWriter(fileStream))
                    {
                        streamWriter.WriteLine("eof");
                    }
                }

                Assert.AreEqual("file26" + Environment.NewLine + "eof" + Environment.NewLine, File.ReadAllText(fi.FullName));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #59
0
        public void TestReadAllTextWithEncoding()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file26.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);
            try
            {
                using (var streamWriter = File.CreateText(tempLongPathFilename, Encoding.Unicode))
                {
                    streamWriter.WriteLine("file26");
                }

                Assert.AreEqual("file26" + Environment.NewLine, File.ReadAllText(fi.FullName, Encoding.Unicode));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Exemple #60
0
        public void TestCreateText()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file20.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);
            Assert.IsFalse(fi.Exists);

            using (fi.CreateText())
            {
            }

            try
            {
                Assert.IsTrue(File.Exists(fi.FullName)); // don't use FileInfo.Exists, it caches existance
            }
            finally
            {
                fi.Delete();
            }
        }