예제 #1
0
        private void AlphaFS_Directory_EnumerateAlternateDataStreams(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var folder = tempRoot.RandomDirectoryFullPath;

                Console.WriteLine("Input Directory Path: [{0}]", folder);


                var myStream            = "ӍƔŞtrëƛɱ-" + tempRoot.RandomString;
                var myStream2           = "myStreamTWO-" + tempRoot.RandomString;
                var allStreams          = new[] { myStream, myStream2 };
                var streamStringContent = "(1) Computer: [" + Environment.MachineName + "]" + "\tHello there, " + Environment.UserName;

                Console.WriteLine("\nA directory is created and {0} streams are added.", allStreams.Length.ToString(CultureInfo.CurrentCulture));

                var di = new Alphaleonis.Win32.Filesystem.DirectoryInfo(folder);
                di.Create();


                const int defaultStreamsDirectory = 0; // The default number of data streams for a folder.
                var       currentNumberofStreams  = di.EnumerateAlternateDataStreams().Count();

                Assert.AreEqual(defaultStreamsDirectory, currentNumberofStreams, "Total amount of default streams do not match.");
                Assert.AreEqual(currentNumberofStreams, Alphaleonis.Win32.Filesystem.Directory.EnumerateAlternateDataStreams(folder).Count(), "Total amount of Directory.EnumerateAlternateDataStreams() streams do not match.");


                // Create alternate data streams.
                // Because of the colon, you must supply a full path and use PathFormat.FullPath or PathFormat.LongFullPath,
                // to prevent a: "NotSupportedException: path is in an invalid format." exception.

                var stream1Name = folder + Alphaleonis.Win32.Filesystem.Path.StreamSeparator + myStream;
                var stream2Name = folder + Alphaleonis.Win32.Filesystem.Path.StreamSeparator + myStream2;

                Alphaleonis.Win32.Filesystem.File.WriteAllLines(stream1Name, UnitTestConstants.StreamArrayContent, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);
                Alphaleonis.Win32.Filesystem.File.WriteAllText(stream2Name, streamStringContent, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);



                var newNumberofStreams = Alphaleonis.Win32.Filesystem.Directory.EnumerateAlternateDataStreams(folder).Count();
                Console.WriteLine("\n\nNew stream count: [{0}]", newNumberofStreams);


                // Enumerate all streams from the folder.
                foreach (var stream in di.EnumerateAlternateDataStreams())
                {
                    Assert.IsTrue(UnitTestConstants.Dump(stream, -10));
                }


                // Show the contents of our streams.
                Console.WriteLine();
                foreach (var streamName in allStreams)
                {
                    Console.WriteLine("\n\tStream name: [{0}]", streamName);


                    // Because of the colon, you must supply a full path and use PathFormat.FullPath or PathFormat.LongFullPath,
                    // to prevent a: "NotSupportedException: path is in an invalid format." exception.

                    foreach (var line in Alphaleonis.Win32.Filesystem.File.ReadAllLines(folder + Alphaleonis.Win32.Filesystem.Path.StreamSeparator + streamName, Alphaleonis.Win32.Filesystem.PathFormat.FullPath))
                    {
                        Console.WriteLine("\t\t{0}", line);
                    }
                }



                // Show DirectoryInfo instance data of the streams.

                var dirInfo = new Alphaleonis.Win32.Filesystem.DirectoryInfo(stream1Name, Alphaleonis.Win32.Filesystem.PathFormat.LongFullPath);

                Console.WriteLine();
                UnitTestConstants.Dump(dirInfo, -17);



                // Show FileInfo instance data of the streams.

                var fileInfo1 = new Alphaleonis.Win32.Filesystem.FileInfo(stream1Name, Alphaleonis.Win32.Filesystem.PathFormat.LongFullPath);
                var fileInfo2 = new Alphaleonis.Win32.Filesystem.FileInfo(stream2Name, Alphaleonis.Win32.Filesystem.PathFormat.LongFullPath);

                Console.WriteLine();
                UnitTestConstants.Dump(fileInfo1, -17);
                UnitTestConstants.Dump(fileInfo2, -17);


                Assert.AreEqual(myStream, fileInfo1.Name);
                Assert.AreEqual(myStream2, fileInfo2.Name);

                Assert.IsNull(fileInfo1.EntryInfo);
                Assert.IsNull(fileInfo2.EntryInfo);
            }

            Console.WriteLine();
        }
        private void Directory_EnumerateAlternateDataStreams(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = Alphaleonis.Win32.Filesystem.Path.GetTempPath("Directory-EnumerateAlternateDataStreams-" + UnitTestConstants.GetRandomFileNameWithDiacriticCharacters());

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }

            const int defaultStreamsDirectory = 0; // The default number of data streams for a folder.

            Console.WriteLine("\nInput Directory Path: [{0}]", tempPath);
            Console.WriteLine("\nA directory is created and {0} streams are added.", UnitTestConstants.AllStreams.Count());


            try
            {
                var di = new Alphaleonis.Win32.Filesystem.DirectoryInfo(tempPath);
                di.Create();

                var currentNumberofStreams = di.EnumerateAlternateDataStreams().Count();

                Assert.AreEqual(defaultStreamsDirectory, currentNumberofStreams, "Total amount of default streams do not match.");
                Assert.AreEqual(currentNumberofStreams, Alphaleonis.Win32.Filesystem.Directory.EnumerateAlternateDataStreams(tempPath).Count(), "Total amount of Directory.EnumerateAlternateDataStreams() streams do not match.");


                // Create alternate data streams.
                // Because of the colon, you must supply a full path and use PathFormat.FullPath or PathFormat.LongFullPath,
                // to prevent a: "NotSupportedException: path is in an invalid format." exception.

                Alphaleonis.Win32.Filesystem.File.WriteAllLines(tempPath + Alphaleonis.Win32.Filesystem.Path.StreamSeparator + UnitTestConstants.MyStream, UnitTestConstants.StreamArrayContent, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);
                Alphaleonis.Win32.Filesystem.File.WriteAllText(tempPath + Alphaleonis.Win32.Filesystem.Path.StreamSeparator + UnitTestConstants.MyStream2, UnitTestConstants.StreamStringContent, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);


                var newNumberofStreams = Alphaleonis.Win32.Filesystem.Directory.EnumerateAlternateDataStreams(tempPath).Count();
                Console.WriteLine("\n\nCurrent stream Count(): [{0}]", newNumberofStreams);


                // Enumerate all streams from the folder.
                foreach (var stream in di.EnumerateAlternateDataStreams())
                {
                    Assert.IsTrue(UnitTestConstants.Dump(stream, -10));
                }


                // Show the contents of our streams.
                Console.WriteLine();
                foreach (var streamName in UnitTestConstants.AllStreams)
                {
                    Console.WriteLine("\n\tStream name: [{0}]", streamName);

                    // Because of the colon, you must supply a full path and use PathFormat.FullPath or PathFormat.LongFullPath,
                    // to prevent a: "NotSupportedException: path is in an invalid format." exception.

                    foreach (var line in Alphaleonis.Win32.Filesystem.File.ReadAllLines(tempPath + Alphaleonis.Win32.Filesystem.Path.StreamSeparator + streamName, Alphaleonis.Win32.Filesystem.PathFormat.FullPath))
                    {
                        Console.WriteLine("\t\t{0}", line);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (UNEXPECTED) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                Assert.IsTrue(false);
            }
            finally
            {
                Alphaleonis.Win32.Filesystem.Directory.Delete(tempPath);
                Assert.IsFalse(Alphaleonis.Win32.Filesystem.Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed.");
            }

            Console.WriteLine();
        }