C # Path.GetTempPath returns error "Part of path could not be found"

I download a file from ftp server and save it to the directory defined in Path.GetTempPath (); however, I am getting the following error: Could not find part of the path.

I have verified that the returned path is correct: C: \ Users \ [username] \ AppData \ Local \ Temp.

SYSTEM, Administrators, and [username] have full control over this directory. I thought the point of the temp directory was that it was open to anything / everyone to save, but just in case I gave permission to change the NETWORK SERVICE network. (I'm assuming the ASP.NET Dev username is being used, but I'm not sure.)

I am using VS 08 for Vista.

Here's my code:

FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(
    ConfigurationManager.AppSettings["FTPServer"] + "//" + fileName);

downloadRequest.Credentials = new NetworkCredential(
    ConfigurationManager.AppSettings["FTPUsername"],
    ConfigurationManager.AppSettings["FTPPassword"]);

downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse downloadResponse = 
    (FtpWebResponse)downloadRequest.GetResponse();

try
{
    Stream downloadStream = downloadResponse.GetResponseStream();

    if (downloadStream != null) 
    {
        logger.Info("File Download status: {0}", 
            downloadResponse.StatusDescription);

        StreamReader downloadReader = new StreamReader(downloadStream);

        try
        {
            if (downloadReader != null)
            {

                StreamWriter downloadWriter = 
                    new StreamWriter(Path.GetTempPath());
                downloadWriter.AutoFlush = true;
                downloadWriter.Write(downloadReader.ReadToEnd());
            }
        }
        finally
        {
            if (downloadReader != null)
            {
                downloadReader.Close();
            }
        }
    }
}
finally
{
    if (downloadResponse != null)
    {
        downloadResponse.Close();
    }
}

      

I really appreciate any ideas on what I am doing wrong here.

Thank!

+2


source to share


2 answers


It looks like you need to add the filename to the end of the temporary path. Try the following:



StreamWriter downloadWriter =
    new StreamWriter(Path.Combine(Path.GetTempPath(), fileName));

      

+5


source


StreamWriter downloadWriter = 
                    new StreamWriter(Path.GetTempPath());

      

You are trying to open the StreamWriter in a directory, not a file. If you want the name of the temp file use Path.GetTempFileName()

:



StreamWriter downloadWriter = 
                    new StreamWriter(Path.GetTempFileName());

      

Either that or do what the Skinniest Man said.

+2


source







All Articles