当前位置: 首页>>代码示例>>C#>>正文


C# FileSystemWatcher类代码示例

本文整理汇总了C#中FileSystemWatcher的典型用法代码示例。如果您正苦于以下问题:C# FileSystemWatcher类的具体用法?C# FileSystemWatcher怎么用?C# FileSystemWatcher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


FileSystemWatcher类属于命名空间,在下文中一共展示了FileSystemWatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FileSystemWatcher_Renamed_Negative

    public static void FileSystemWatcher_Renamed_Negative()
    {
        using (var dir = Utility.CreateTestDirectory())
        using (var watcher = new FileSystemWatcher())
        {
            // put everything in our own directory to avoid collisions
            watcher.Path = Path.GetFullPath(dir.Path);
            watcher.Filter = "*.*";
            AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Renamed);

            watcher.EnableRaisingEvents = true;

            // run all scenarios together to avoid unnecessary waits, 
            // assert information is verbose enough to trace to failure cause

            // create a file
            using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
            using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir")))
            {
                // change a file
                testFile.WriteByte(0xFF);
                testFile.Flush();

                // deleting a file & directory by leaving the using block
            }

            Utility.ExpectNoEvent(eventOccured, "created");
        }
    }
开发者ID:GitTorre,项目名称:corefx,代码行数:29,代码来源:FileSystemWatcher.Renamed.cs

示例2: Main

	static void Main (string [] args)
	{
		string base_dir = AppDomain.CurrentDomain.BaseDirectory;
		string watch_dir = Path.Combine (base_dir, "watch");

		FileSystemWatcher watcher = new FileSystemWatcher ();
		watcher.Path = watch_dir;
		watcher.NotifyFilter |= NotifyFilters.Size;
		watcher.Created += new FileSystemEventHandler (Created);
		watcher.Deleted += new FileSystemEventHandler (Deleted);
		watcher.Changed += new FileSystemEventHandler (Changed);
		watcher.Renamed += new RenamedEventHandler (Renamed);

		File.Create (Path.Combine (watch_dir, "tmp")).Close ();

		watcher.EnableRaisingEvents = true;
		Thread.Sleep (200);

		File.Move (Path.Combine (watch_dir, "tmp"), Path.Combine (watch_dir, "tmp2"));
		Thread.Sleep (200);

		Assert.AreEqual (1, _events.Count, "#A");

		RenamedEventArgs renamedArgs = _events [0] as RenamedEventArgs;
		Assert.IsNotNull (renamedArgs, "#B1");
		Assert.AreEqual (WatcherChangeTypes.Renamed, renamedArgs.ChangeType, "#B2");
		Assert.AreEqual (Path.Combine (watch_dir, "tmp2"), renamedArgs.FullPath, "#B3");
		Assert.AreEqual ("tmp2", renamedArgs.Name, "#B4");
		Assert.AreEqual (Path.Combine (watch_dir, "tmp"), renamedArgs.OldFullPath, "#B5");
		Assert.AreEqual ("tmp", renamedArgs.OldName, "#B6");
	}
开发者ID:mono,项目名称:gert,代码行数:31,代码来源:test.cs

示例3: FileSystemWatcher_Directory_Delete_DeepDirectoryStructure

        public void FileSystemWatcher_Directory_Delete_DeepDirectoryStructure()
        {
            // List of created directories
            List<TempDirectory> lst = new List<TempDirectory>();

            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
            using (var watcher = new FileSystemWatcher(dir.Path, "*"))
            {
                watcher.IncludeSubdirectories = true;
                watcher.NotifyFilter = NotifyFilters.DirectoryName;

                // Priming directory
                lst.Add(new TempDirectory(Path.Combine(dir.Path, "dir")));

                // Create a deep directory structure and expect things to work
                for (int i = 1; i < 20; i++)
                {
                    // Test that the creation triggers an event correctly
                    string dirPath = Path.Combine(lst[i - 1].Path, String.Format("dir{0}", i));
                    Action action = () => Directory.Delete(dirPath);
                    Action cleanup = () => Directory.CreateDirectory(dirPath);
                    cleanup();

                    ExpectEvent(watcher, WatcherChangeTypes.Deleted, action, cleanup);

                    // Create the directory so subdirectories may be created from it.
                    lst.Add(new TempDirectory(dirPath));
                }
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:31,代码来源:FileSystemWatcher.Directory.Delete.cs

示例4: FileSystemWatcher_File_Delete_DeepDirectoryStructure

        public void FileSystemWatcher_File_Delete_DeepDirectoryStructure()
        {
            // List of created directories
            List<TempDirectory> lst = new List<TempDirectory>();

            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
            using (var watcher = new FileSystemWatcher(dir.Path, "*"))
            {
                watcher.IncludeSubdirectories = true;
                watcher.NotifyFilter = NotifyFilters.FileName;

                // Create a deep directory structure
                lst.Add(new TempDirectory(Path.Combine(dir.Path, "dir")));
                for (int i = 1; i < 20; i++)
                {
                    string dirPath = Path.Combine(lst[i - 1].Path, String.Format("dir{0}", i));
                    lst.Add(new TempDirectory(dirPath));
                }

                // Put a file at the very bottom and expect it to raise an event
                string fileName = Path.Combine(lst[lst.Count - 1].Path, "file");
                Action action = () => File.Delete(fileName);
                Action cleanup = () => File.Create(fileName).Dispose();
                cleanup();

                ExpectEvent(watcher, WatcherChangeTypes.Deleted, action, cleanup, fileName);
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:29,代码来源:FileSystemWatcher.File.Delete.cs

示例5: FileSystemWatcher_ctor

 public static void FileSystemWatcher_ctor()
 {
     string path = String.Empty;
     string pattern = "*.*";
     using (FileSystemWatcher watcher = new FileSystemWatcher())
         ValidateDefaults(watcher, path, pattern);
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:7,代码来源:FileSystemWatcher.unit.cs

示例6: FileSystemWatcher_IncludeSubDirectories_Directory

    public static void FileSystemWatcher_IncludeSubDirectories_Directory()
    {
        using (var dir = Utility.CreateTestDirectory())
        using (var watcher = new FileSystemWatcher(Path.GetFullPath(dir.Path)))
        {
            string dirPath = Path.GetFullPath(dir.Path);
            string subDirPath = Path.Combine(dirPath, "subdir");

            watcher.Path = dirPath;
            watcher.IncludeSubdirectories = true;
            AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created);

            Directory.CreateDirectory(subDirPath);

            watcher.EnableRaisingEvents = true;

            Directory.CreateDirectory(Path.Combine(subDirPath, "1"));
            Utility.ExpectEvent(eventOccured, "created");

            watcher.IncludeSubdirectories = false;
            Directory.CreateDirectory(Path.Combine(subDirPath, "2"));
            Utility.ExpectNoEvent(eventOccured, "created");

            watcher.IncludeSubdirectories = true;
            Directory.CreateDirectory(Path.Combine(subDirPath, "3"));
            Utility.ExpectEvent(eventOccured, "created");
        }
    }
开发者ID:johnhhm,项目名称:corefx,代码行数:28,代码来源:FileSystemWatcher.IncludeSubDirectories.cs

示例7: FileSystemWatcher_ctor_path_pattern

 public static void FileSystemWatcher_ctor_path_pattern()
 {
     string path = @".";
     string pattern = "honey.jar";
     using (FileSystemWatcher watcher = new FileSystemWatcher(path, pattern))
         ValidateDefaults(watcher, path, pattern);
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:7,代码来源:FileSystemWatcher.unit.cs

示例8: Watch

    public void Watch(string source, string target)
    {
        sourceFolder = source;
        targetFolder = target;

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = source;
        watcher.IncludeSubdirectories = true;

        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        // Adds a filter
        watcher.Filter = "*.*";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }
开发者ID:arthurbsh,项目名称:SyncFolder,代码行数:27,代码来源:Watcher.cs

示例9: ConstructingWithPathAndFilter

 public void ConstructingWithPathAndFilter()
 {
     FileSystemWatcher tested = new FileSystemWatcher(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
       Assert.IsNotNull(tested);
       Assert.AreEqual(AppDomain.CurrentDomain.BaseDirectory, tested.Path);
       Assert.AreEqual("*.dll", tested.Filter);
 }
开发者ID:CodeFork,项目名称:PluginFramework-1,代码行数:7,代码来源:UnitTest_FileSystemWatcher.cs

示例10: ConstructingWithPathShouldInitLog

 public void ConstructingWithPathShouldInitLog()
 {
     System.IO.FileInfo thisAssemblyPath = new System.IO.FileInfo(GetType().Assembly.Location);
       FileSystemWatcher tested = new FileSystemWatcher(thisAssemblyPath.Directory.FullName);
       ILogWriter logWriter = tested as ILogWriter;
       Assert.IsNotNull(logWriter.Log);
 }
开发者ID:CodeFork,项目名称:PluginFramework-1,代码行数:7,代码来源:UnitTest_FileSystemWatcher.cs

示例11: Start

    bool flagFileChanged;   // for file watcher
    // Use this for initialization
    void Start () {
        parseConfigFile();

        absolutePath = Path.GetFullPath(absolutePath);
        // Prepare the audio files
        reloadSounds();
        clips = new List<AudioClip>();
        
        objprefab = (GameObject) Resources.Load("Lightbulbprefab");
        balls = new List<GameObject>();
        for (int j = 0; j<NUM_OF_BALLS; j++) balls.Add(Instantiate(objprefab));
        // File watcher
        locSoundFile = "location_sound.txt"; // the file initially watch
        watcher = new FileSystemWatcher();
        watcher.Path = ".";
        // Watch for change of LastWrite
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        // watch all similar files
        watcher.Filter = "location_sound*.txt";
        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
        flagFileChanged = false;

    }
开发者ID:forest-jiang,项目名称:Unity-3DSound-generator-3DCeption,代码行数:28,代码来源:main_controller.cs

示例12: FileSystemWatcher_File_NotifyFilter_Attributes

        public void FileSystemWatcher_File_NotifyFilter_Attributes(NotifyFilters filter)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var file = new TempFile(Path.Combine(testDirectory.Path, "file")))
            using (var watcher = new FileSystemWatcher(testDirectory.Path, Path.GetFileName(file.Path)))
            {
                watcher.NotifyFilter = filter;
                var attributes = File.GetAttributes(file.Path);

                Action action = () => File.SetAttributes(file.Path, attributes | FileAttributes.ReadOnly);
                Action cleanup = () => File.SetAttributes(file.Path, attributes);

                WatcherChangeTypes expected = 0;
                if (filter == NotifyFilters.Attributes)
                    expected |= WatcherChangeTypes.Changed;
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && ((filter & LinuxFiltersForAttribute) > 0))
                    expected |= WatcherChangeTypes.Changed;
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && ((filter & OSXFiltersForModify) > 0))
                    expected |= WatcherChangeTypes.Changed;
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && ((filter & NotifyFilters.Security) > 0))
                    expected |= WatcherChangeTypes.Changed; // Attribute change on OSX is a ChangeOwner operation which passes the Security NotifyFilter.
                
                ExpectEvent(watcher, expected, action, cleanup, file.Path);
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:25,代码来源:FileSystemWatcher.File.NotifyFilter.cs

示例13: Run

    public static void Run()
    {
        // Create a new FileSystemWatcher and set its properties.
        watcher = new FileSystemWatcher();
       
        try
        {
            Console.WriteLine("Directory: ");
            String name;
            name = Console.ReadLine();
            watcher.Path = name;

            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Watch all files
            watcher.Filter = "*.*";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            //set to watch subfolders
            watcher.IncludeSubdirectories = true;
            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }
        catch (SystemException) { Console.WriteLine("Wrong folder"); Console.ReadKey(); return; }
    }
开发者ID:ernvalentino88,项目名称:pds_project,代码行数:35,代码来源:Program.cs

示例14: Run

    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();

        // If a directory is not specified, exit program.
        if (args.Length != 2)
        {
            // Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"C:\x";
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "geojson.json";

        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        // Only watch text files.

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnDeleted);;
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;
    }
开发者ID:fjkish,项目名称:DotNet,代码行数:35,代码来源:Program.cs

示例15: FileSystemWatcher_Created_MoveDirectory

    public static void FileSystemWatcher_Created_MoveDirectory()
    {
        // create two test directories
        using (TemporaryTestDirectory dir = Utility.CreateTestDirectory(),
            targetDir = new TemporaryTestDirectory(dir.Path + "_target"))
        using (var watcher = new FileSystemWatcher("."))
        {
            string testFileName = "testFile.txt";

            // watch the target dir
            watcher.Path = Path.GetFullPath(targetDir.Path);
            watcher.Filter = testFileName;
            AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created);

            string sourceFile = Path.Combine(dir.Path, testFileName);
            string targetFile = Path.Combine(targetDir.Path, testFileName);

            // create a test file in source
            File.WriteAllText(sourceFile, "test content");

            watcher.EnableRaisingEvents = true;

            // move the test file from source to target directory
            File.Move(sourceFile, targetFile);

            Utility.ExpectEvent(eventOccured, "created");
        }
    }
开发者ID:jsalvadorp,项目名称:corefx,代码行数:28,代码来源:FileSystemWatcher.Created.cs


注:本文中的FileSystemWatcher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。