Not able to access a folder from a web application and able to access it after restarting the machine.‏
<p>I have a web application. In that we can upload a file to a location in two ways.</p>

<ol>
<li><p>We have a Fileupload control from which user can upload a file to a location(eg:\testmachine\share) which is shared to everyone.</p></li>
<li><p>We also have a automated job, which does this automatically. So when we place the file at a particular file location (eg: \testmachine2\share2) or FTP , the job will download this file to the shared location (eg:\testmachine\share).</p></li>
</ol>

<p>But sometimes, the user is not able to upload the file using the first scenario. The entire issue is happened on production servers where i dont have any access.
While am working on this i got the trace.Below is the trace.</p>

<pre><code>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)
</code></pre>

<p>Now i tried to reproduce the same behavior in my local machine and am able to reproduce this scenario. </p>

<p>Here is the Code:</p>

<pre><code>public static IList&lt;DownloadInfo&gt; Download(XmlDataDocument xDoc)
    {
      List&lt;DownloadInfo&gt; downloadedList = new List&lt;DownloadInfo&gt;();

      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 &amp;&amp; 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&lt;DownloadInfo&gt; 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);
        }
      }
    }
</code></pre>

<p>But when i restarted the machine in which the shared folder exists, am no more getting the issue.</p>

<p>Can any one please help me on to fix this and also its good to reproduce the issue.</p>

<p>Thanks,</p>

<p>Vinay.</p>
