C# 如何将任何操作从运行在PC1中的一个应用程序传递到运行在PC2中的不同应用程序

C# 如何将任何操作从运行在PC1中的一个应用程序传递到运行在PC2中的不同应用程序,c#,winforms,visual-studio-2010,visual-studio,action,C#,Winforms,Visual Studio 2010,Visual Studio,Action,一个应用程序就是filewatcher,它一直在后台运行。。当添加任何新文件时,它会更新数据库。此应用程序仅更新数据库的两个字段。主键部件和**附件** 同样,我有一个不同的主应用程序,它由不同PC中的不同用户处理。因此,此应用程序有一个传输按钮,在数据库中更新任何新文件时可以移动。此按钮的操作将导致文件从单元格[2]移动到单元格[3]。这意味着它将所有文件从位置C:\user\fab移动到位置C:\user\release。此应用程序没有任何文件监视程序 所以我的问题是: 文件监视程序正在完美

一个应用程序就是filewatcher,它一直在后台运行。。当添加任何新文件时,它会更新数据库。此应用程序仅更新数据库的两个字段。主键部件和**附件** 同样,我有一个不同的主应用程序,它由不同PC中的不同用户处理。因此,此应用程序有一个传输按钮,在数据库中更新任何新文件时可以移动。此按钮的操作将导致文件从单元格[2]移动到单元格[3]。这意味着它将所有文件从位置C:\user\fab移动到位置C:\user\release。此应用程序没有任何文件监视程序

所以我的问题是:

文件监视程序正在完美地更新数据库中的所有内容。但是,当其他用户在单个**传输按钮的帮助下移动文件时,请按**。在另一台PC上运行的文件监视程序认为该文件已被删除,并执行删除数据库的命令。任何解决办法。请帮帮我

链接第一部分:

第二部分:

代码段:

    private void OnChanged(object source, FileSystemEventArgs e)
    {

        switch (e.ChangeType)
        {
            case WatcherChangeTypes.Created:
                //Insert file in database
                this.Invoke(addItemInList, "File: " + e.FullPath + " Created");
                {
                    string filename = Path.GetFileName(e.FullPath);
                    filename = filename.Substring(0, filename.LastIndexOf("."));
                    SqlConnection con = new SqlConnection(@"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=CncDB;User ID=CncDbUser;password=gcodedata");
                    con.Open();
                    SqlCommand cmd = new SqlCommand(@"INSERT INTO cncinfo (part,draftpath) VALUES ('" + filename + "','" + e.FullPath + "') ", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                break;
            case WatcherChangeTypes.Deleted:
                //remove file from database
                this.Invoke(addItemInList, "File: " + e.FullPath + " Deleted");
                {
                    string filename = Path.GetFileName(e.FullPath);
                    filename = filename.Substring(0, filename.LastIndexOf("."));
                    SqlConnection con = new SqlConnection(@"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=CncDB;User ID=CncDbUser;password=gcodedata");
                    con.Open();
                    SqlCommand cmd = new SqlCommand(@"delete cncinfo where part='" + filename + "'  ;", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                break;
            case WatcherChangeTypes.Changed:
                ///if you are storing file in database(not file name whole file in binary format)
                ///then you can update the file in database here
                ///this event will be fired when any data has changed in the file.
                this.Invoke(addItemInList, "File: " + e.FullPath + " Changed");
                {
                    string filename = Path.GetFileName(e.FullPath);
                    filename = filename.Substring(0, filename.LastIndexOf("."));
                    SqlConnection con = new SqlConnection(@"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=CncDB;User ID=CncDbUser;password=gcodedata");
                    con.Open();
                    SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + filename
                        + "',draftpath='" + e.FullPath + "' where part='" + filename + "'", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    this.Validate();
                }
                break;
        }
    }

    private void OnRenamed(object source, RenamedEventArgs e)
    {
        //Update then old filename with new here
        this.Invoke(addItemInList, string.Format("File: {0} renamed to {1}", e.OldFullPath, e.FullPath));
        {
            //string extension = e.FullPath.Substring(e.FullPath.LastIndexOf("."));
            string oldFileName = Path.GetFileName(e.OldFullPath);
            oldFileName = oldFileName.Substring(0, oldFileName.LastIndexOf("."));
            string filename = Path.GetFileName(e.FullPath);
            filename = filename.Substring(0, filename.LastIndexOf("."));
            SqlConnection con = new SqlConnection(@"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=CncDB;User ID=CncDbUser;password=gcodedata");
            con.Open();
            SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + filename + "',draftpath='" + e.FullPath + "' where part='" + oldFileName + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            this.Validate();
        }
    }

您的文件监视程序只有3个操作。您的文件监视程序是否以某种方式加载文件夹的历史记录?换句话说,如果您的文件监视程序没有保留历史数据,您可以杀死Filewatcher.exe,移动文件,然后重新启用。这种方法的问题是,当exe关闭时,您可能会错过文件夹中的更改或创建

第二种解决方案是复制文件并重命名原始文件,在文件名末尾追加FILEMOVED202或任何唯一字符串,以便Test.doc将是TestFILEMOVED202.doc,并在文件查看器中启动更改功能。在更改后的函数中,可以使用字符串函数指定字符串的文件名。如果它存在于change函数中,则从file watcher中删除实际文件,使用另一个标志禁止删除日志记录,并使用值moved或其他任何内容更新数据库


在file watcher中,您可以随时访问的唯一内容是文件名,而不是文件内容,因此我将使用它作为您的标志工具。

转到第二个按下传输按钮的应用程序。使该代码将文件复制到传输目标,而不是移动它,这样删除函数就不会启动。复制文件后,重命名原始文件,并在文件名中添加任何唯一的字符串。与Test.docx类似,TestFileMovedTrans.doc将成为TestFileMovedTrans.doc。这将触发您的文件监视程序的更改事件,它将放入上面的更改代码中。在该更改代码中,检查文件名是否为唯一字符串FileMovedTrans或其他内容。如果该字符串是move函数引起的&您可以记录并删除该文件


一旦删除了更改代码中的文件,将再次触发Filewatcher删除代码,您将再次检查文件名中的字符串。如果字符串存在,则不要将其记录为已删除,因为它只是被移动了,如果字符串不在文件名中,则只需使用常规删除功能即可

您还应该为移动文件的目标文件夹声明另一个filewatcher。但是,正如我告诉你的,ASP.NETRoom你必须对特定的文件进行签名,也就是说,结合文件大小、修改日期和文件类型,你可以创建一个类似FileSignature的类

并在发生删除文件事件时将该类的对象创建到列表中

private List<FileSignature> movedList = new List<FileSignature>();
private void Watcher1_OnChange()
{
    ....
    case Delete:
        FileSignature singature = new FileSignature();
        FileInfo info = new System.IO.FileInfo(e.FullPath);
        signature.FileSize = info.Length;
        signature.ModifiedDate = info.LastWriteTime;
        singature.FileType = info.Extension;
        signature.OldFileName = e.FullPath;
        movedList.Add(signature);
        break;

}
现在,代码的主要部分在这里

现在,您需要一个标志,当整个过程完成文件移动时,该标志将从主应用程序更新。您可以使用另一个FileWatcher

当您从主应用程序获得进程已被删除的标志时,您可以调用另一个方法从数据库中删除movedList中存在的项


我无法得到您的第二个解决方案转到第二个应用程序,该应用程序按下了传输按钮。使该代码将文件复制到传输目标,而不是移动它,这样删除函数就不会启动。复制文件后,重命名原始文件,并在文件名中添加任何唯一的字符串。与Test.docx类似,TestFileMovedTrans.doc将成为TestFileMovedTrans.doc。这将触发您的文件监视程序的更改事件,它将放入上面的更改代码中。在该更改代码中,检查文件名是否为唯一字符串FileMovedTrans或其他内容。如果该字符串是由move函数引起的&你可以记录并删除该文件。我知道你的答案将是最好的答案。。我甚至不需要运行这个代码。。它将是任何事物的完美!!!非常感谢您为我生成有价值的代码P
private List<FileSignature> movedList = new List<FileSignature>();
private void Watcher1_OnChange()
{
    ....
    case Delete:
        FileSignature singature = new FileSignature();
        FileInfo info = new System.IO.FileInfo(e.FullPath);
        signature.FileSize = info.Length;
        signature.ModifiedDate = info.LastWriteTime;
        singature.FileType = info.Extension;
        signature.OldFileName = e.FullPath;
        movedList.Add(signature);
        break;

}
private void Watcher2_OnChange()
{
    ....
    case Created:
        FileInfo info = new FileInfo(e.FullPath);
        FileSignature[] found = (from FileSignature sign in movedList 
                          where sign.ModifiedDate == info.LastWriteTime &&
                                sign.FileType == info.Extension &&
                                sign.FileSize == info.Length
                         select sign).ToArray()
        if (found.Length > 0)  //if the file found then it will be known as moved file otherwise created.
        {
            FileSignature signature = found[0]; //we will take only one file from the index 0. coz the file watcher event will occur for every file separately.
            //remove that file from the list first. So, further undeleted item in the list will be known as deleted at the end.
            movedList.Remove(signature);

            string oldFileName = signature.OldFileName; //this will contain full path of file which has been moved to new location.
            string newFileName = e.FullPath; //this will return new file name which is moved from source directory
            //Now you can perform you database update process here for move file. 
            //you have both path and flag that file is moved.
        } 
        else
        {
            //Now you can perform you database update process here for create file. 
        }

        break;    
}
private void DeleteItems()
{
     foreach (FileSignature signature in movedList)
    {
         string filename = signature.OldFileName
         //Delete from the database. coz this files are known as deleted but not moved to destination 
    }
}