C# 如何在filewatcher中定义窗口登录?

C# 如何在filewatcher中定义窗口登录?,c#,asp.net,filesystemwatcher,C#,Asp.net,Filesystemwatcher,我开发了一个asp.NETC文件监视程序来监视文件夹 如果有任何修改的或新创建的文件,程序将把这些文件复制到另一个Windows server共享文件夹,例如。\192.168.12.12\shared using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using Syste

我开发了一个asp.NETC文件监视程序来监视文件夹

如果有任何修改的或新创建的文件,程序将把这些文件复制到另一个Windows server共享文件夹,例如。\192.168.12.12\shared

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.Globalization;
using System.Text.RegularExpressions;

namespace FileMonitor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            fileWatcher.Path = @txtPath.Text;
            fileWatcher.Filter = txtFilter.Text;
            fileWatcher.IncludeSubdirectories = chkSubdirectories.Checked;
        }

        DateTime lastRead = DateTime.MinValue;

        private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); 

            if (lastWriteTime != lastRead)
            {


                txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
                txtLog.Focus();
                txtLog.Select(txtLog.TextLength, 0);
                txtLog.ScrollToCaret();

                try
                {
                    string myPath = e.FullPath;
                    string myFile = e.Name;

                    System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile);

                    string myAttibs = myFileInfo.Attributes.ToString();

                    System.IO.File.Copy(myPath, @"\\192.168.12.12\\shared\\" + myFile, true);

                    lastRead = lastWriteTime; 

                }
                catch (System.IO.IOException ex)
                {
                    System.IO.IOException myex = ex;
                }
                catch (System.Exception ex)
                {
                    System.Exception myex = ex;
                }

            }
        }

        private void fileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);

            if (lastWriteTime != lastRead)
            {


                txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
                txtLog.Focus();
                txtLog.Select(txtLog.TextLength, 0);
                txtLog.ScrollToCaret();

                try
                {
                    string myPath = e.FullPath;
                    string myFile = e.Name;

                    System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile);

                    string myAttibs = myFileInfo.Attributes.ToString();

                    System.IO.File.Copy(myPath, @"\\192.168.12.12\\shared\\" + myFile, true);

                    lastRead = lastWriteTime;

                }
                catch (System.IO.IOException ex)
                {
                    System.IO.IOException myex = ex;
                }
                catch (System.Exception ex)
                {
                    System.Exception myex = ex;
                }

            }
        }

    }
}
但我发现在运行Windows explorer中的filewatcher程序(如type\192.168.12.12\shared)之前需要访问共享文件夹一次,它会提示我输入登录名和密码,否则程序无法复制到共享文件夹

如何在我的程序中传递此登录名和密码,使其可以访问共享文件夹


谢谢。

通常的方法是将共享文件夹上的必要权限授予运行此类复制代码的用户


如果无法执行此操作,则需要使用Win32函数,或者为了验证共享,请指向pinvoke.net的链接,该链接显示了如何在C中使用这些函数。

在大量使用的系统上,FileWatcher将报告“文件夹”更改,而不是文件更改。您不能使用完整路径来检测确切的更改。您需要创建文件列表并手动检测差异。