实现文件监视器 [英] Implement File Watcher

查看:90
本文介绍了实现文件监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我第一次在网络服务上工作,我必须将文件FTP到特定位置。所有文件都是由单个位置x中的另一个方法生成的。文件的前3个字符是我们如何识别属于哪个客户端的文件。每个客户端都有2个要FTP的文件。



从这个x位置我们必须获取这些文件,并从数据库中获取客户端特定的详细信息,如clientname,landingzone ,ftpusername等,



以下是我写的内容。首先,我从Web服务中获取GetClientInformation方法的所有客户端,然后编写for循环以访问服务的属性。

然后我检查x位置中的文件。



在这里我必须等待10个小时并检查我的所需文件是否为prsent或者每隔10分钟。如果特定客户的2个文件存在,那么我们将ftp文件。



有人可以告诉我接下来应该做什么。



Hi,

I am working on the webservices for the first time where i have to FTP the Files to a specific location. All the files are generated by another method in a single location x.The first 3 characters of the file is how we identify the file belonging to which client. Every client will have 2 files to be FTPied.

From this x location we have to get these files and also get the client specific details from the database like clientname,landingzone,ftpusername etc.,

Below is what i wrote. first i am getting all the clients from GetClientInformation method in a webservice then wrote a for loop in order to access the properties of the service.
then i am checking for the files present in the x location.

Here i have to wait for 10 hours and check every 10 mins if my required file is prsent or not.f the 2 files for the specific client are present then we will ftp the files.

Could some one please let me know what should be done next.

        public bool Transmit()
        {
            try
            {
                strClientEncounterInfo = generateFile.GetClientInformation();
                foreach (var client in strClientEncounterInfo)
                {
                    strClientID=client.ClientEncounterId.ToString().Substring(0,3);
                    if (IsReportRequested == true)
                    {
                        var files = Directory.GetFiles(ConfigurationManager.AppSettings["ECG_LOCATION"], strClientID).Where(item => item.StartsWith(strClientID));
                        if (files.Count() == 2)
                        {
//FTP the files

                        }
                        else
                        {
isFilePresent(ConfigurationManager.AppSettings["ECG_LOCATION"], filename);
                        }

                    }
                }
            }
        }
        public bool isFilePresent(string path, string filename)
        {
            try
            {
                watcher = new FileSystemWatcher();
                watcher.Path = path;
                watcher.Filter = filename + "*.*";
                watcher.NotifyFilter = NotifyFilters.LastAccess |NotifyFilters.LastWrite |NotifyFilters.FileName |NotifyFilters.Size;
                watcher.Changed += new FileSystemEventHandler(OnChanged);
                watcher.Created += new FileSystemEventHandler(OnChanged);
                watcher.Deleted += new FileSystemEventHandler(OnChanged);
                //watcher.Renamed += new RenamedEventHandler(OnRenamed);
                watcher.EnableRaisingEvents = true;    
            }
            catch (Exception)
            {
                
                throw;
            }
            return true;
        }
        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            
        }

推荐答案

在考虑下一步之前,请尝试了解编程的一些基础知识。看看你的异常处理:

Before thinking about next step, try to understand some basics of programming. Look at your exception handling:
try
{
    //...
}
catch (Exception)
{
    throw;
}

这显然是胡说八道。从逻辑上讲,这相当于根本不处理异常(除了浪费一些额外的资源)。这就是你真正需要做的。不应该在每种情况下都有例外情况。恰恰相反,它们应该很少处理,只有在战略选择的点(我称之为能力点),你知道如何处理它们,通常,在每个线程的顶层堆栈框架,或者,对于UI ,在主事件循环的特殊点。异常旨在与正常的指令流隔离。在许多情况下可以使用重新投掷,但前提是处理程序执行某些有用的操作。请看我过去的答案:

扔。 .then ... rethrowing [ ^ ]。



-SA

This is obvious nonsense. Logically, this is equivalent of not handling exceptions at all (except wasting some extra resources). And this is what you really need to do. Exceptions should not be caught in every context. Just the opposite, they should be handled quite rarely, only in strategically chosen points (I call them "competence points") where you know how to handle them, and, usually, on a top stack frame of each thread, or, for UI, in special points of the main event loop. Exceptions are designed to be isolated from normal instruction flow. Re-throwing can be used in many cases, but only if the handler does something useful. Please see my past answer:
throw . .then ... rethrowing[^].

—SA


这篇关于实现文件监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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