如何在手动创建文件到目录时找到真实路径

时间:2016-06-28 07:57:53

标签: c# filesystemwatcher

我遇到了麻烦,我有一个Directory和一个FileSystemWatcher控制在那里移动和创建的文件,我必须CheckSum它们,当我移动文件时这很好,但当我尝试创建一个新的文本文档并给它一个名称,该程序将路径作为“... \ new text document.txt”而不是我给它的名称,我该如何解决这个问题?

    private void watch()
    {

        watcher.NotifyFilter = NotifyFilters.LastAccess |                                                                        NotifyFilters.LastWrite
                    | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnCreated);
        watcher.Deleted += new FileSystemEventHandler(OnDeleted);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);
        watcher.EnableRaisingEvents = true;
    }

    private void OnCreated(object sender, FileSystemEventArgs e)
    {
        MessageBox.Show("Nuovo file creato", "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);            
        string creato = e.FullPath;
        string cod = CheckSum(creato);
    }

    private string CheckSum(string fpath)
    {
        using (var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead(fpath))
            {
                return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); 
            }
        }
    }

0 个答案:

没有答案