我在 c#(asp.net) 中有一个代码,其中 File.exists() 当我在 IIS 7.5 中执行它时返回一个假值,但当我通过 Visual Studio 调试在我的本地主机中运行它时返回一个真值。
我已经检查了所有的读写权限,并且所有 IIS 用户都拥有该文件夹的所有权限。
任何人都可以提出任何解决此问题的方法吗?
附上代码供参考:-
protected void downloadFiles(string nric)
{
string serverPath = Server.MapPath(@"~\Temp\");
string folderName = nric;
string dirName = serverPath + folderName;
string logFilePath = Server.MapPath(@"~\Temp\");
string logFile = logFilePath + "log2.txt";
FileStream fs = new FileStream(logFile, FileMode.Create, FileAccess.ReadWrite);
StreamWriter logWriter = new StreamWriter(fs);//File.CreateText(logFile);
logWriter.WriteLine("Preparing to export ....." + DateTime.Now.ToString());
try
{
logWriter.WriteLine("entered try loop");
Directory.CreateDirectory(dirName + "\\CollectionsItems");
DataTable dt = new DataTable();
dh.Fill("ACP", dt, "P_ACP_COLLECTION_VIEW_SEARCH_OWN", nric, "", "", "");
foreach (DataRow dRow in dt.Rows)
{
logWriter.WriteLine("entered for loop");
string fileURL = dRow["FILE_URL"].ToString();
logWriter.WriteLine("file URL =" + fileURL);
string collectionFolder = dRow["COLLECTION_FOLDER"].ToString();
string collectionFile = Path.Combine(ConfigurationManager.AppSettings.Get("root") + "\\" + nric + "\\" + collectionFolder, fileURL);
//string orgFile = collectionFile.Replace("\\", "/");
string orgFile = collectionFile;
string filName = dirName + "\\CollectionsItems\\" + fileURL;
logWriter.WriteLine("org file =" + orgFile);
logWriter.WriteLine("file name =" + filName);
//WebClient wc = new WebClient();
//wc.DownloadFile(new Uri(orgFile), filName);
if (File.Exists(orgFile))
{
logWriter.WriteLine("entered if loop");
FileStream readStream = new FileStream(orgFile, FileMode.Open, FileAccess.ReadWrite);
FileStream writeStream = new FileStream(filName, FileMode.Create, FileAccess.Write);
ReadWriteStream(readStream, writeStream);
//File.Copy(orgFile,filName,true);
}
}
logWriter.WriteLine(" ");
logWriter.WriteLine("Exporting Completed ....." + DateTime.Now.ToString());
logWriter.Flush();
logWriter.Close();
}
catch (Exception ex)
{
}
}
以下是在本地主机和 IIS 中生成的日志文件。
localhost:- 准备导出 .....4/30/2013 5:03:46 PM 输入 try loop 输入循环文件 URL =CO201304262025117208.jpg org 文件 =C:\inetpub\wwwroot\imprints3\Temp\demostu01\如果为循环文件 URL =CO201304231616496601.jpg 组织文件 =C:\inetpub\www\root\imprint Temp\demostu01\Achievements\CO201304231616496601.jpg 文件名 =C:\inetpub\wwwroot\Cybersphere_imprints\Temp\demostu01\CollectionsItems\CO201304231616496601.jpg 如果为循环文件输入循环 URL =CO201303281418567609\docx org 文件,则输入wwwroot\imprints3\Temp\demostu01\Achievements\CO201303281418567609.docx 文件名 =C:\inetpub\wwwroot\Cybersphere_imprints\Temp\demostu01\CollectionsItems\CO201303281418567609.docx
导出完成.....4/30/2013 5:03:46 PM
IIS:- 准备导出 .....4/30/2013 5:05:57 PM 输入 try loop 输入循环文件 URL =CO201304262025117208.jpg org 文件 =C:\inetpub\wwwroot\imprints3\Temp\demostu01\成就\CO201304262025117208.jpg 文件名 =C:\inetpub\wwwroot\imprints3\Temp\demostu01\CollectionsItems\CO201304262025117208.jpg 输入循环文件 URL =CO201304231616496601.jpg 组织文件 =C:\inetpub\www01\imprints3\Temp\de \Achievements\CO201304231616496601.jpg 文件名 =C:\inetpub\wwwroot\imprints3\Temp\demostu01\CollectionsItems\CO201304231616496601.jpg 输入循环文件 URL =CO201303281418567609.docx 组织文件 =C:\inetpub\wwwroot\imprints3\ demostu01\Achievements\CO201303281418567609.docx 文件名 =C:\inetpub\wwwroot\imprints3\Temp\demostu01\CollectionsItems\CO201303281418567609.docx
出口完成.....4/30/2013 5:05:57 PM
可以看出,日志文件的不同之处在于它们没有进入 if 循环,这很奇怪,因为文件存在并且相同的函数正在我的本地主机中进入 if 循环。