一般说来,对于文件的操作不外是本地和远程两种。而本地实现上可分虚拟路径下与绝对路径,远程可分域用户管理与工作组状态下。
本地的文件操作,虚拟路径略,因为如果绝对路径可以的话那么本地的任何一个文件都可操作了。这个比较简单,注意两点。如果是xp系统,默认iis服务器的帐户是aspnet,所以你要在你要操作的文件夹的安全选项上添加它的权限。如果是win2003,默认的帐户则是Network Service,对应加上权限就可以了。
远程的文件操作比较麻烦,如果是域用户状态下,可能以下方法可行:(我没有实验)
1 在Web.config中设置
<identity impersonate="true" userName="machinename\userName" password="password" />
2 模拟登陆
使用已经存在的域用户来登录(而且为了与其它程序界面一致一定要使用 Forms 登录),查找了一些相关的资料发现还是可以实现的。
主要还是依靠 advapi32.dll 中的 LogonUser API 函数。
using System.Web.Security;
using System.Runtime.InteropServices;
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
void Login_Click(Object sender, EventArgs E)
{
IntPtr token = IntPtr.Zero;
if(LogonUser(UserName.Value,
UserDomain.Value,
UserPass.Value,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref token) != 0)
{
FormsAuthentication.RedirectFromLoginPage(UserName.Value,
PersistCookie.Checked);
}
else
{
lblResults.Text = "Invalid Credentials: Please try again";
}
}
如果非域用户状态下,可以这样更改machine.config,操作如下:
改其中的password为“pass”,web服务器帐户设为aspnet,密码改为“pass”,远程文件服务器aspnet帐户密码也改为“pass”。并且在你要操作的文件夹上加aspnet的权限。
附MSDN说明:
可以使用以下任一方法,从 ASP.NET 应用程序中访问远程资源:
附WebService文件操作代码:
[WebMethod]
public void MonitorFolder()
{
//Get folder's location
string sql = "select * from filepath where foldername='MonitorFolder' or foldername='Image'";
//DataSet ds = DbHelperSQL.Query(sql);
//string strSourceFloder = ds.Tables[0].Rows[0]["Email"].ToString();
//string strDestFolder = ds.Tables[0].Rows[0]["Email"].ToString();
string strSourceFloder = @"\\192.168.168.101\ljt";
string strDestFolder = @"\\192.168.168.102\share";
//Check new package is existed or not
string[] filesSourceFloder = Directory.GetFiles(strSourceFloder);
if (filesSourceFloder.Length > 0)
{
try
{
foreach (string s in filesSourceFloder)
{
string extendName = s.Substring(s.Length - 3, 3);
string commondName = s.Substring(s.LastIndexOf("\\") + 1, s.LastIndexOf(".") - s.LastIndexOf("\\")-1);
if (extendName.ToLower() == "tif")
{
//Move
string strSourceNameTxt = strSourceFloder +"\\"+ commondName+".txt";
string strSourceNameImage = strSourceFloder + "\\" + commondName + ".tif";
string strDestNameTxt = strDestFolder + "\\" + commondName + ".txt";
string strDestNameImage = strDestFolder + "\\" + commondName + ".tif";
File.Move(strSourceNameTxt, strDestNameTxt);
File.Move(strSourceNameImage, strDestNameImage);
//int ss = Convert.ToInt16("sdf");
//Insert a record to database
string[] strResult = commondName.Split(new Char[] { '~' });
string ReceiveNumber = strResult[0];
string SendNumber = strResult[1];
string ReceiveDate = strResult[2];
string PageCount = strResult[3];
sql = @"insert into faxinfo (filename,pathfortxt,pathforimage,pathforxml,status,errormessage,
xmltempid,receivenumber,sendnumber,pagecount,receivedate)
values('" + commondName + "','" + strDestFolder + "','" + strDestFolder + "','null',1,0,0,'" + ReceiveNumber + "','" + SendNumber + "','" + PageCount + "','" + ReceiveDate + "')";
DbHelperSQL.ExecuteSql(sql);
}
}
}
catch
{
//Error in inserting a record
string[] filesDestFolder = Directory.GetFiles(strDestFolder);
if (filesDestFolder.Length > 0)
{
//Find the latest file
FileInfo Latest = new FileInfo(filesDestFolder[0]);
foreach (string s in filesDestFolder)
{
FileInfo objFI = new System.IO.FileInfo(s);
if (objFI.CreationTime > Latest.CreationTime)
{
Latest = objFI;
}
}
string commondName = Latest.Name.Substring(0, Latest.Name.Length-4);
sql = "select count(1) from faxinfo where filename='" + commondName + "'";
if (!DbHelperSQL.Exists(sql))
{
//If record is not existed,then move file back
string strSourceNameTxt = strDestFolder + "\\" + commondName + ".txt";
string strSourceNameImage = strDestFolder + "\\" + commondName + ".tif";
string strDestNameTxt = strSourceFloder + "\\" + commondName + ".txt";
string strDestNameImage = strSourceFloder + "\\" + commondName + ".tif";
File.Move(strSourceNameTxt, strDestNameTxt);
File.Move(strSourceNameImage, strDestNameImage);
}
}
}
}
}