在ASP.net中使用FileSystemWatcher [英] Use FileSystemWatcher in ASP.net

查看:160
本文介绍了在ASP.net中使用FileSystemWatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为控制台应用程序创建了文件系统监视程序.它工作正常.

I have created File system watcher for console application. it is working flawless.

只有按"q"键,它才会连续列出要添加文件的文件夹,并在找到文件时显示文件名.

unliess you press 'q' its keep listning the folder for adding files and display name of the files when found.

public void FileWatcher()
        {
            while (true)
            {

                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = @"C:\\WATCH-FOLDER";
                watcher.IncludeSubdirectories = true;

                watcher.NotifyFilter = NotifyFilters.Attributes |
                NotifyFilters.CreationTime |
                NotifyFilters.DirectoryName |
                NotifyFilters.FileName |
                NotifyFilters.LastAccess |
                NotifyFilters.LastWrite |
                NotifyFilters.Security |
                NotifyFilters.Size;

                watcher.Filter = "*.*";

                watcher.Changed += new FileSystemEventHandler(OnChanged);
                watcher.Created += new FileSystemEventHandler(OnChanged);

                watcher.EnableRaisingEvents = true;
            }

        }

        public void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("{0}, with path {1} has been {2}", e.Name, e.FullPath, e.ChangeType);
        }

        public void OnRenamed(object source, RenamedEventArgs e)
        {

            Console.WriteLine(" {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }

但是现在我该如何在我的Web应用程序中使用它.我想要的是,无论何时将文件添加到它选择的文件夹中,然后在数据库中插入信息.因此,当我按Show all(全部显示)时,此信息将成为表中现有数据的一部分.

but now how can i use this in my web application. what i want, when ever file adds in folder it picked and and insert info in database. so when i press show all so this info would be part of existing data in table.

但是我不知道在哪里放置函数.有人说将其放置在Global.asax文件中,而有人说将其放置在主页或添加线程中.我完全感到困惑,不知道该怎么做.

But i have no idea where to put function. Some people says put it in Global.asax file, and some says put in main page, or add thread. I am completly confused and have no idea how to do that.

推荐答案

您为什么不在global.asap的app_start中托管此文件监视程序,因此即使iis在闲置20分钟后关闭了您的应用程序,它也会重新启动每当用户再次点击您的应用程序时,都会有一个新的观察者.

Why don't you host this file watcher in app_start in global.asap, so even if iis shut down your app after 20min of inactivity, then it will relaunch a new watcher whenever a user hit your application again.

这是一个有效的解决方案,并且对于那些不拥有服务器的人来说,它可能是唯一的解决方案. e.谁将他们的网站托管在共享托管上,却无权访问Windows服务

This is a valid solution, and can be the only solution for those who don't own the server, i. e. Who host their web on shared hosting and have no access to Windows services

这篇关于在ASP.net中使用FileSystemWatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆