我开发了一个 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,它会提示我输入登录名和密码),否则程序无法复制到共享文件夹。
我怎样才能在我的程序中传递这个登录名和密码才能访问共享文件夹?
谢谢。