System.IO.Directory.SetLastWriteTime C# (CSharp) Method

SetLastWriteTime() public static method

public static SetLastWriteTime ( string path, System lastWriteTime ) : void
path string
lastWriteTime System
return void
        public static void SetLastWriteTime(string path, System.DateTime lastWriteTime) { }
        public static void SetLastWriteTimeUtc(string path, System.DateTime lastWriteTimeUtc) { }

Usage Example

示例#1
0
        public async Task Load()
        {
            using (var testDirectory = new TestDirectory())
            {
                var directory = new Directory(testDirectory.FullPath, null, new TestItemFactory());

                const string emptyDirectoryName       = "EmptyDirectory";
                var          emptyDirectoryLastUpdate = new DateTime(2019, 2, 2, 15, 30, 20);
                var          emptyDirectoryPath       = testDirectory.CreateDirectory(emptyDirectoryName, emptyDirectoryLastUpdate);

                const string notEmptyDirectoryName       = "NotEmptyDirectory";
                var          notEmptyDirectoryLastUpdate = new DateTime(2019, 4, 4, 16, 35, 25);
                var          notEmptyDirectoryPath       = testDirectory.CreateDirectory(notEmptyDirectoryName);

                const string file1Name       = "File1";
                var          file1LastUpdate = new DateTime(2018, 3, 3, 12, 0, 30);
                const string file2Name       = "File2";
                var          file2LastUpdate = new DateTime(2019, 2, 3, 11, 5, 10);
                TestDirectory.CreateFiles(notEmptyDirectoryPath, new Dictionary <string, DateTime>
                {
                    { file1Name, file1LastUpdate },
                    { file2Name, file2LastUpdate }
                });

                // Только после добавления файлов в директорию, так как дата перетёрлась бы.
                IODirectory.SetLastWriteTime(notEmptyDirectoryPath, notEmptyDirectoryLastUpdate);

                const string rootFileName       = "RootFile";
                var          rootFileLastUpdate = new DateTime(2019, 2, 5, 8, 5, 0);
                testDirectory.CreateFiles(new Dictionary <string, DateTime> {
                    { rootFileName, rootFileLastUpdate }
                });

                await directory.Load();

                Assert.True(directory.IsLoaded);
                Assert.Equal(2, directory.Items.Length); // Один файл и одна не пустая директория.

                // Сначала идёт директория, а потом файл.
                Assert.IsType <Directory>(directory.Items[0]);
                Assert.IsType <TestFile>(directory.Items[1]);

                Assert.Equal(notEmptyDirectoryName, directory.Items[0].Name);
                Assert.Equal(notEmptyDirectoryPath, directory.Items[0].FullPath);
                Assert.Equal(notEmptyDirectoryLastUpdate, directory.Items[0].LastUpdate);
                Assert.True(((IDirectory)directory.Items[0]).IsLoaded);
                Assert.Equal(2, ((IDirectory)directory.Items[0]).Items.Length);
                var file1 = ((IDirectory)directory.Items[0]).Items[0];
                Assert.Equal(file1Name, file1.Name);
                Assert.Equal(file1LastUpdate, file1.LastUpdate);
                var file2 = ((IDirectory)directory.Items[0]).Items[1];
                Assert.Equal(file2Name, file2.Name);
                Assert.Equal(file2LastUpdate, file2.LastUpdate);

                Assert.Equal(rootFileName, directory.Items[1].Name);
                Assert.Equal(rootFileLastUpdate, directory.Items[1].LastUpdate);
            }
        }
All Usage Examples Of System.IO.Directory::SetLastWriteTime