本文介绍了File Watcher错误已更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Windows服务,该服务使用文件监视程序来监视路径C:\ Temp,以进行任何文件创建,重命名,删除,更改.

我使用文件系统监视程序组件来执行以下操作:为此.

我使用了自己的另一个Windows服务在路径C:\ Temp中显示旧文件,并且可以为所选文件创建zip.

保存文件"对话框要求用户创建一个zip文件.例如,是否在C:\ Temp中有诸如aa.txt,ms.txt ......之类的文件.

如果我给"aa"作为要在其中创建的zip文件名C:\ Temp在保存文件对话框中,然后显示以下错误

找不到路径C:\ Temp \ aa

此错误是由文件系统监视程序生成的创建例外

我的代码是

private void fileSystemWatcherCreated(object sender, FileSystemEventArgs e)
        {

            try
            {  
               string[] Attributes = new string[10];
                if (e.ChangeType == WatcherChangeTypes.Created)//Checking whether the file watcher type is created
                {
                    if (Directory.Exists(e.FullPath))//if new directory created store it's info into string array
                    {
                        DirectoryInfo dirInfo = new DirectoryInfo(e.FullPath);
                        Regex regExpression = new Regex(@"\\", RegexOptions.RightToLeft);
                        string[] dirName = regExpression.Split(e.FullPath, 2);
                        Attributes[0] = dirName[1];
                        Attributes[1] = dirInfo.FullName;
                        Attributes[2] = "0";
                        Attributes[3] = dirInfo.CreationTime.ToString();
                        Attributes[4] = dirInfo.LastWriteTime.ToString();
                        Attributes[5] = dirInfo.LastAccessTime.ToString();
                        Attributes[6] = dirInfo.Extension;
                        Attributes[7] = "Directory";
                        DirectoryInfo dirDetails = new DirectoryInfo(dirInfo.FullName);
                        DirectorySecurity getAccess = dirDetails.GetAccessControl();
                        IdentityReference ident = getAccess.GetOwner(typeof(NTAccount));
                        Attributes[8] = ident.Value.ToString();
                        Attributes[9] = DateTime.Now.ToString();
                        dbObject.loadingInfo(Attributes);//function call to populate these info into table
                    }
                    else
                    {
                           //if new filecreated store it's info into string array
                            FileInfo fileInfo = new FileInfo(e.FullPath);
                            string fullPath = e.FullPath;
                            Regex regExpression = new Regex(@"\\", RegexOptions.RightToLeft);
                            string[] fileName = regExpression.Split(e.FullPath, 2);
                            Attributes[0] = fileName[1];
                            Attributes[1] = fileInfo.FullName;
                            Attributes[2] = fileInfo.Length.ToString();//Line at which exception thrown
                            Attributes[3] = fileInfo.CreationTime.ToString();
                            Attributes[4] = fileInfo.LastWriteTime.ToString();
                            Attributes[5] = fileInfo.LastAccessTime.ToString();
                            Attributes[6] = fileInfo.Extension;
                            Attributes[7] = "File";
                            FileInfo fileDetais = new FileInfo(fileInfo.FullName);
                            FileSecurity getAccess = fileDetais.GetAccessControl();
                            IdentityReference ident = getAccess.GetOwner(typeof(NTAccount));
                            Attributes[8] = ident.Value.ToString();
                            Attributes[9] = DateTime.Now.ToString();
                            dbObject.loadingInfo(Attributes);//function call to populate these info into table
                    }
                }

            }
            catch (Exception error)//To handle any exception occurred in creation of file or directory
            {
                MessageBox.Show(error.Message, "New File Creation Error");//Error is "Could not find the path C:\Temp\aa"
            }
        }















推荐答案


这篇关于File Watcher错误已更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:10