基于SharePoint的文件监控系统F2S

F2S是一个与MicrosoftSharePoint集成的文件监控系统,它通过监视特定文件夹,将文件名添加到SharePoint列表或上传到SharePoint文档库。该系统采用多线程设计,能够在发现新文件时不间断运行,并且通过自定义DLL文件(SPWebServiceLink)与SharePoint进行连接。

自2005年起,就开始使用SharePoint。为一家媒体公司开发了这个应用程序,该公司有文件服务器用于保存其入口剪辑(每小时超过1000个剪辑)。公司将其数据服务器切换到SharePoint,其中一个重要需求是将剪辑名称输入到SharePoint列表中。为他们开发了这个应用程序,它解决了他们的问题,很高兴听到这个应用程序自2008年以来一直在运行,无需任何调试。

这是一个完整的应用程序,无法描述所有的细节,但会尝试与分享一些最重要的问题。如果决定在公司中使用这个应用程序作为解决方案,也可以帮助。

mainForm是应用程序的核心,是应用程序的主窗体,所有其他用户控件都在mainForm的主面板"pnlMain"中打开。在mainForm的左侧栏有一个名为"OpenFormTree"的自定义控件,也可以在这个应用程序中看到它的代码。这个控件保存打开表单的信息,并为应用程序提供了表单浏览的能力。

Main form有一个Watchers属性,它是一个ArrayList,保存所有Watcher控件的信息。如上所述,在本应用程序中,可以有一个或多个watchers。每个watcher监视一个指定的文件夹,并将文件名上传到Sharepoint列表或上传文件到Sharepoint文档库。

当应用程序运行时,将调用main form的Load处理程序。此方法还调用另一个方法"RestartWatching()",该方法从数据库读取所有watchers信息并加载到Watchers属性中。

private void RestartWatching() { foreach(TreeNode t in this.openFormTree1.Nodes) { if(t.Text=="Watching Status...") { MessageBox.Show("You have to close SSP Watching now & Run again.", "Restart Watching", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); return; } } _Watchers = new System.Collections.ArrayList(); SSPWatchFolderDatatsetTableAdapters.SSPWatchFolder_FoldersTableAdapter adp = new F2S_WS.SSPWatchFolderDatatsetTableAdapters.SSPWatchFolder_FoldersTableAdapter(); SSPWatchFolderDatatset dst = new SSPWatchFolderDatatset(); adp.Fill(dst.SSPWatchFolder_Folders); foreach(SSPWatchFolderDatatset.SSPWatchFolder_FoldersRow r in dst.SSPWatchFolder_Folders.Rows) { try { SPWebServiceLink.WS_SPList li = new SPWebServiceLink.WS_SPList(r.Folder_SiteURL, _NC); string rootFolder = li.GetRootFolder(r.Folder_ListID); Uri u = new Uri(r.Folder_SiteURL); string rootUrl = u.Scheme + "//" + u.Host + ":" + u.Port.ToString(); F2S_WS.Controls.WatcherController _watcher = new F2S_WS.Controls.WatcherController(); _watcher.WatcherLastCheck = r.Folder_LastCheck; _watcher.WatcherListID = r.Folder_ListID; _watcher.WatcherName = rootFolder; _watcher.rootURL = rootUrl; _watcher.WatcherPath = r.Folder_Path; _watcher.WatcherSiteTimeDif = r.Folder_TimeDif; _watcher.WatcherSiteURL = r.Folder_SiteURL; _watcher.WatcherMode = r.Folder_Mode; _watcher._NC = _NC; _watcher.FSWStart(); _Watchers.Add(_watcher); } catch { } } }

Watcher控件具有以下属性,包括所有所需的数据:

public string WatcherPath = "-"; public DateTime WatcherLastCheck = new DateTime(); public string WatcherName = "-"; public string WatcherListID = "-"; public string WatcherListName = "-"; public string WatcherWebID = "-"; public string WatcherWebName = "-"; public string WatcherSiteURL = "-"; long WatcherSiteTimeDif = 0; public string WatcherMode = "List"; public bool Started = false; public string rootURL; public FileSystemWatcher fsw; public System.Net.NetworkCredential _NC; private SPWebServiceLink.WS_SPList _SPList; private SPWebServiceLink.WS_DocumentWorkSpace _DocumentWorkSpace; private SPWebServiceLink.WS_Copy _Copy; private DataSet listDataset;

Watcher控件基于"FileSystemWatcher"工作,配置在FSWStart()方法中。如以下代码所示,在FSWStart()方法中,所有配置和设置都已完成,并且开始监视。

public void FSWStart() { fsw = new FileSystemWatcher(); try { fsw.Path = this.WatcherPath; fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; fsw.Filter = "*.*"; fsw.Created += new FileSystemEventHandler(OnChanged); fsw.IncludeSubdirectories = true; fsw.EnableRaisingEvents = true; fsw.SynchronizingObject = txtStatus; WriteNewLineLog("Watcher Started ..."); _SPList = new SPWebServiceLink.WS_SPList(WatcherSiteURL, _NC); _DocumentWorkSpace = new SPWebServiceLink.WS_DocumentWorkSpace(WatcherSiteURL, _NC); _Copy = new SPWebServiceLink.WS_Copy(WatcherSiteURL, _NC); listDataset = _SPList.GetListSchema(WatcherListID); this.Started = true; timer1.Enabled = true; } catch(System.Exception ex) { this.Started = false; fsw.EnableRaisingEvents = false; WriteNewLineLog("Watcher can not be started ..."); WriteNewLineLog("F2S ERROR - er453#e - " + ex.Message); } }

每个Watcher都有自己的FileSystemWatcher,因此每个Watcher有两个处理程序来处理FileSystemWatcher事件。

public void OnChanged(object source, FileSystemEventArgs e) { try { WriteNewLineLog("Watcher found new file, Named: " + e.FullPath); if(WatcherMode == "List") { string[] sp1 = { "\\" }; string[] folders = e.Name.Split(sp1, StringSplitOptions.RemoveEmptyEntries); CreateListTodayFolder(); if(System.IO.File.Exists(e.FullPath)) { UploadListItem(e.FullPath, ""); } else { UpLoadDirectoryContentsToList(e.FullPath, 1); } // ... System.IO.File.WriteAllText("c:\\t.txt", colName, System.Text.Encoding.UTF8); WriteNewLineLog("Watcher create new item in Link List"); } else { string[] sp1 = { "\\" }; string[] folders = e.Name.Split(sp1, StringSplitOptions.RemoveEmptyEntries); CreateTodayFolder(); if(System.IO.File.Exists(e.FullPath)) { UpLoadFile(e.FullPath, ""); } else { UpLoadDirectoryContents(e.FullPath, 1); } } } catch(Exception ex) { string ss = ex.Message; WriteNewLineLog("F2S ERROR - 1f56"); } }

SSPW_FoldersList:这个用户控件允许用户管理需要监视和控制的文件夹。

SSP_WatcherControllerList:这个用户控件包含Watcher数组列表中的所有活动监视控件。

沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485