Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从web应用程序访问文件夹,并且无法在重新启动计算机后访问该文件夹;_C# - Fatal编程技术网

C# 无法从web应用程序访问文件夹,并且无法在重新启动计算机后访问该文件夹;

C# 无法从web应用程序访问文件夹,并且无法在重新启动计算机后访问该文件夹;,c#,C#,我有一个web应用程序。我们可以通过两种方式将文件上载到某个位置 我们有一个Fileupload控件,用户可以从该控件将文件上载到每个人共享的位置(例如:\testmachine\share) 我们还有一个自动作业,它会自动完成这项工作。因此,当我们将文件放置在特定的文件位置(例如:\testmachine2\share2)或FTP时,作业会将该文件下载到共享位置(例如:\testmachine\share) 但有时,用户无法使用第一种方案上载文件。整个问题都发生在我无法访问的生产服务器上。 当

我有一个web应用程序。我们可以通过两种方式将文件上载到某个位置

  • 我们有一个Fileupload控件,用户可以从该控件将文件上载到每个人共享的位置(例如:\testmachine\share)

  • 我们还有一个自动作业,它会自动完成这项工作。因此,当我们将文件放置在特定的文件位置(例如:\testmachine2\share2)或FTP时,作业会将该文件下载到共享位置(例如:\testmachine\share)

  • 但有时,用户无法使用第一种方案上载文件。整个问题都发生在我无法访问的生产服务器上。 当我在做这件事的时候,我得到了线索。下面是线索

    Access to the path **\\testmachine\share\test.zip** is denied.,mscorlib, at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode)
    at System.Web.HttpPostedFile.SaveAs(String filename)
    at System.Web.UI.WebControls.FileUpload.SaveAs(String filename)
    
    现在我试着在本地机器上重现同样的行为,并且能够重现这个场景

    代码如下:

    public static IList<DownloadInfo> Download(XmlDataDocument xDoc)
        {
          List<DownloadInfo> downloadedList = new List<DownloadInfo>();
    
          WebClient request = new WebClient();
    
          string user = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("user").Value;
          string password = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("password").Value;
          //domain is not used Curretly. This might be useful when UNC is implemented in authenticated mode.
          string domain = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("domain").Value;
    
          request.Credentials = new NetworkCredential(user, password);
    
          FileStream file = null;
          try
          {
            string serverType = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("type").Value;
            string serverPath = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("path").Value;
            if (serverType.Equals("ftp"))
            {
              if (!serverPath.ToLower().StartsWith("ftp://"))
              {
                serverPath = "ftp://" + serverPath.Trim();
              }
              else
              {
                serverPath = serverPath.Trim();
              }
              FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri(serverPath));
              fwr.Credentials = request.Credentials;
              fwr.Method = WebRequestMethods.Ftp.ListDirectory;
              StreamReader sr = new StreamReader(fwr.GetResponse().GetResponseStream());
              string str = sr.ReadLine();
              while (str != null)
              {
                if (str.ToUpper().EndsWith(".ZIP"))
                {
                  DownloadInfo dwnInfo = new DownloadInfo();
                  dwnInfo.SourceServerZipFilePath = str;
                  downloadedList.Add(dwnInfo);
                }
                str = sr.ReadLine();
              }
              // construct the server path, if file location is provided instead directory location.
              if (downloadedList.Count == 1)
              {
                if (serverPath.EndsWith(downloadedList[0].SourceServerZipFilePath))
                {
                  string[] delimiter = { downloadedList[0].SourceServerZipFilePath };
                  serverPath = serverPath.Split(delimiter, StringSplitOptions.RemoveEmptyEntries)[0];
                }
              }
              sr.Close();
              sr = null;
              fwr = null;
    
            }
    
            else if (serverType.Equals("file"))
            {
              //TODO in authenticated mode.
              if (!serverPath.ToLower().StartsWith(@"\\"))
              {
                serverPath = Path.GetFullPath(@"\\" + serverPath.Trim());
              }
              else
              {
                serverPath = Path.GetFullPath(serverPath.Trim());
              }
    
              DirectoryInfo dInfo = new DirectoryInfo(serverPath);
              FileInfo fInfo = new FileInfo(serverPath);
              if (dInfo.Exists)
              {
                FileInfo[] filelist = dInfo.GetFiles("*.zip");
                foreach (FileInfo f in filelist)
                {
                  DownloadInfo dwnInfo = new DownloadInfo();
                  dwnInfo.SourceServerZipFilePath = f.Name;
                  downloadedList.Add(dwnInfo);
                }
    
              }
              else if (fInfo.Exists && fInfo.Extension.ToUpper() == ".ZIP")
              {
                DownloadInfo dwnInfo = new DownloadInfo();
                dwnInfo.SourceServerZipFilePath = fInfo.Name;
                downloadedList.Add(dwnInfo);
                serverPath = fInfo.DirectoryName;
              }
              else if (!dInfo.Exists || !fInfo.Exists)
                Logger.Error(String.Format("{0} is not accessible. Make sure the folder exists and the machine account where the system agent is installed has access to the folder.", serverPath));
            }
    
            if (downloadedList.Count == 0)
              Logger.Warn(string.Format("{0} does not have a ZIP file. Make sure the folder contains the ZIP file for each dataset.", serverPath));
    
            //Copy files to destination location (upload folder)
            foreach (DownloadInfo dwnInfo in downloadedList)
            {
              string strFile = dwnInfo.SourceServerZipFilePath;
              DateTime time = new DateTime();
              time = DateTime.Now;
              string date = time.ToString("yyyyMMdd-HHmmss_");
    
              Uri UriPath = new Uri(serverPath + "/" + strFile);
              byte[] filedata = request.DownloadData(UriPath);
              // create the destination path.
              string destPath = xDoc.SelectSingleNode("configurationSettings/fileDestinationServer").Attributes.GetNamedItem("path").Value;
              destPath = Path.Combine(destPath.Trim(), date + strFile);
    
              file = File.Create(destPath);
              file.Write(filedata, 0, filedata.Length);
              file.Close();
              file = null;
              //changing source server path to full path. Earlier only file name was assigned.
              dwnInfo.SourceServerZipFilePath = UriPath.OriginalString;
              dwnInfo.DestinationServerZipFilePath = destPath;
              //System.Console.WriteLine(strFile + " - Download Complete.");
            }
    
            //Extract all the downloded zip files at destination location.
            extractFile(downloadedList);
          }
          catch (Exception ex)
          {
            Logger.Error(String.Format("Exception occured: {0}", ex.Message), ex);
          }
          finally
          {
            if (file != null)
              file.Close();
          }
    
          return downloadedList;
        }
    
        private static void extractFile(IList<DownloadInfo> fileList)
        {
          ZipUtils zip = new ZipUtils();
    
          foreach (DownloadInfo dwnInfo in fileList)
          {
            try
            {
              FileInfo fInfo = new FileInfo(dwnInfo.DestinationServerZipFilePath);
              zip.extract(fInfo.FullName, fInfo.FullName.Replace(fInfo.Extension, ""));
              dwnInfo.DestinationServerUnzipFilePath = fInfo.FullName.Replace(fInfo.Extension, "");
            }
            catch (Exception ex)
            {
              Logger.Error(String.Format("Exception occured during extracting {0}", dwnInfo.SourceServerZipFilePath), ex);
            }
          }
        }
    
    公共静态IList下载(XmlDataDocument xDoc)
    {
    List downloadedList=新列表();
    WebClient请求=新建WebClient();
    字符串user=xDoc.SelectSingleNode(“配置设置/fileSourceServer”).Attributes.GetNamedItem(“用户”).Value;
    字符串password=xDoc.SelectSingleNode(“configurationSettings/fileSourceServer”).Attributes.GetNamedItem(“password”).Value;
    //当前未使用域。这在以身份验证模式实现UNC时可能很有用。
    string domain=xDoc.SelectSingleNode(“配置设置/fileSourceServer”).Attributes.GetNamedItem(“域”).Value;
    request.Credentials=新的网络凭据(用户、密码);
    FileStream file=null;
    尝试
    {
    字符串serverType=xDoc.SelectSingleNode(“配置设置/fileSourceServer”).Attributes.GetNamedItem(“类型”).Value;
    字符串serverPath=xDoc.SelectSingleNode(“configurationSettings/fileSourceServer”).Attributes.GetNamedItem(“path”).Value;
    if(serverType.Equals(“ftp”))
    {
    如果(!serverPath.ToLower().StartsWith(“ftp:/”)
    {
    serverPath=“ftp://”+serverPath.Trim();
    }
    其他的
    {
    serverPath=serverPath.Trim();
    }
    FtpWebRequest fwr=(FtpWebRequest)FtpWebRequest.Create(新Uri(服务器路径));
    fwr.Credentials=请求.Credentials;
    fwr.Method=WebRequestMethods.Ftp.ListDirectory;
    StreamReader sr=新的StreamReader(fwr.GetResponse().GetResponseStream());
    字符串str=sr.ReadLine();
    while(str!=null)
    {
    if(str.ToUpper().EndsWith(“.ZIP”))
    {
    DownloadInfo dwnInfo=新的DownloadInfo();
    dwnInfo.SourceServerZipFilePath=str;
    下载列表。添加(dwnInfo);
    }
    str=sr.ReadLine();
    }
    //如果提供了文件位置而不是目录位置,则构造服务器路径。
    如果(downloadedList.Count==1)
    {
    if(serverPath.EndsWith(downloadedList[0].SourceServerZipFilePath))
    {
    字符串[]分隔符={downloadedList[0].SourceServerZipFilePath};
    serverPath=serverPath.Split(分隔符,StringSplitOptions.RemoveEmptyEntries)[0];
    }
    }
    高级关闭();
    sr=null;
    fwr=null;
    }
    else if(serverType.Equals(“文件”))
    {
    //在身份验证模式下执行TODO。
    如果(!serverPath.ToLower().StartsWith(@“\\”))
    {
    serverPath=Path.GetFullPath(@“\\”+serverPath.Trim());
    }
    其他的
    {
    serverPath=Path.GetFullPath(serverPath.Trim());
    }
    DirectoryInfo dInfo=新的DirectoryInfo(serverPath);
    FileInfo fInfo=新的FileInfo(serverPath);
    if(dInfo.Exists)
    {
    FileInfo[]filelist=dInfo.GetFiles(“*.zip”);
    foreach(文件列表中的文件信息f)
    {
    DownloadInfo dwnInfo=新的DownloadInfo();
    dwnInfo.SourceServerZipFilePath=f.Name;
    下载列表。添加(dwnInfo);
    }
    }
    else if(fInfo.Exists&&fInfo.Extension.ToUpper()==“.ZIP”)
    {
    DownloadInfo dwnInfo=新的DownloadInfo();
    dwnInfo.SourceServerZipFilePath=fInfo.Name;
    下载列表。添加(dwnInfo);
    serverPath=fInfo.DirectoryName;
    }
    否则如果(!dInfo.Exists | |!fInfo.Exists)
    Logger.Error(String.Format(“{0}不可访问。请确保该文件夹存在,并且安装系统代理的计算机帐户可以访问该文件夹。”,serverPath));
    }
    如果(downloadedList.Count==0)
    Logger.Warn(string.Format(“{0}没有ZIP文件。请确保文件夹包含每个数据集的ZIP文件。”,serverPath));
    //将文件复制到目标位置(上载文件夹)
    foreach(下载列表中的下载信息)
    {
    字符串strFile=dwnInfo.SourceServerZipFilePath;
    日期时间=新的日期时间();
    时间=日期时间。现在;
    字符串日期=时间.ToString(“yyyyMMdd-HHmmss”);
    Uri UriPath=新Uri(serverPath+“/”+strFile);
    字节[]文件数据=请求.下载数据(UriPath);
    //创建目标路径。
    字符串destPath=xDoc.SelectSingleNode(“配置设置/fileDestinationServer”).Attributes.GetNamedItem(“路径”).Value;
    destPath=Path.Combine(destPath.Trim(),日期+strFile);
    file=file.Create(destPath);
    file.Write(filedata,0,filedata.Length);
    file.Close();
    file=null;
    //正在将源服务器路径更改为完整路径。以前只分配了文件名。
    dwnInfo.SourceServerZipFilePath=UriPath.OriginalString;
    dwnInfo.DestinationServerZipFilePath=destPath;
    //系统控制台