FileSystemWatcher used to watch for folder/file open

后端 未结 6 1379
太阳男子
太阳男子 2020-11-30 12:06

I have browsed around but cannot find any information on what I am seeking, if there is another post that already goes over this then I apologize.

I am seeking help

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 12:40

    public void OnChanged(object sender, FileSystemEventArgs e)
    {
        string FileName = System.IO.Path.GetFileName(e.FullPath);
    
        if(IsAvailable(System.IO.Path.Combine(RecievedPath,FileName)))
        {
            ProcessMessage(FileName);
        }
    }
    
    
    private void ProcessMessage(string fileName)
    {
        try
        {
           File.Copy(System.IO.Path.Combine(RecievedPath,fileName), System.IO.Path.Combine(SentPath,fileName));
            MessageBox.Show("File Copied");
        }
        catch (Exception)
        { }
    }
    
    private static bool IsAvailable(String filePath)
    {
        try
        {
            using (FileStream inputStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                if (inputStream.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
    
            }
        }
        catch (Exception)
        {
            return false;
        }
    }
    

提交回复
热议问题