File.exists()在localhost中工作,但在IIS 7.5中不工作

时间:2013-04-30 09:25:54

标签: c# asp.net file iis-7.5

我在c#(asp.net)中有一个代码,其中File.exists()在IIS 7.5中执行时返回false值,但是当我通过visual studio调试在localhost中运行它时返回true值。 / p>

我已经检查了所有读写权限,并且所有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中生成的日志文件。

本地主机 - 准备出口..... 4/30/2013 5:03:46 PM 进入try循环 输入循环 文件URL = CO201304262025117208.jpg 组织文件= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ Achievements \ CO201304262025117208.jpg 文件名= C:\ inetpub \ wwwroot \ Cyber​​sphere_imprints \ Temp \ demostu01 \ CollectionsItems \ CO201304262025117208.jpg 输入if循环 输入循环 文件URL = CO201304231616496601.jpg 组织文件= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ Achievements \ CO201304231616496601.jpg 文件名= C:\ inetpub \ wwwroot \ Cyber​​sphere_imprints \ Temp \ demostu01 \ CollectionsItems \ CO201304231616496601.jpg 输入if循环 输入循环 文件URL = CO201303281418567609.docx 组织文件= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ Achievements \ CO201303281418567609.docx 文件名= C:\ inetpub \ wwwroot \ Cyber​​sphere_imprints \ Temp \ demostu01 \ CollectionsItems \ CO201303281418567609.docx

导出已完成..... 4/30/2013 5:03:46 PM

IIS: - 准备出口..... 4/30/2013 5:05:57 PM 进入try循环 输入循环 文件URL = CO201304262025117208.jpg 组织文件= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ Achievements \ CO201304262025117208.jpg 文件名= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ CollectionsItems \ CO201304262025117208.jpg 输入循环 文件URL = CO201304231616496601.jpg 组织文件= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ Achievements \ CO201304231616496601.jpg 文件名= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ CollectionsItems \ CO201304231616496601.jpg 输入循环 文件URL = CO201303281418567609.docx 组织文件= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ Achievements \ CO201303281418567609.docx 文件名= C:\ inetpub \ wwwroot \ imprints3 \ Temp \ demostu01 \ CollectionsItems \ CO201303281418567609.docx

导出已完成..... 4/30/2013 5:05:57 PM

可以看出,日志文件的不同之处在于它们没有进入if循环,这很奇怪,因为文件存在且同一个函数正在我的localhost中进入if循环。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Server.MapPath

if (File.Exists(Server.MapPath(orgFile))

检查here以获取与路径相关的信息。