0

我开发了一个 asp.net c# filewatcher 程序来监视文件夹。

如果有任何修改或新创建的文件,程序会将这些文件复制到另一个窗口服务器共享文件夹,例如 \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;
                }

            }
        }

    }
}

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

我怎样才能在我的程序中传递这个登录名和密码才能访问共享文件夹?

谢谢。

4

1 回答 1

0

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

如果您不能这样做,您需要使用 Win32 函数NetUseAddWNetAddConnection2来对共享进行身份验证(链接指向 pinvoke.net,它显示了如何在 C# 中使用这些函数)。

于 2012-05-01T12:46:28.260 回答