Reputation: 757
I am downloading a file from an ftp server and saving it to the directory defined in Path.GetTempPath(); however, I'm getting the following error: Could not find a part of the path.
I've confirmed that the path returned is correct: C:\Users\[username]\AppData\Local\Temp.
SYSTEM, Administrators, and [username] all have full control over that directory. I thought the point of the temp directory was that it was open for anything/everyone to save to, but just in case, I gave NETWORK SERVICE Modify permissions as well. (I assume that's the username ASP.NET Dev server uses, but I'm not sure.)
I'm using VS 08 on 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'd really appreciate any ideas about what I'm doing wrong here.
Thanks!
Upvotes: 2
Views: 8130
Reputation: 88806
StreamWriter downloadWriter =
new StreamWriter(Path.GetTempPath());
You're trying to open the StreamWriter on a directory rather than on a file. If you want a temp filename, use Path.GetTempFileName()
instead:
StreamWriter downloadWriter =
new StreamWriter(Path.GetTempFileName());
Either that or do what Skinniest Man said.
Upvotes: 2
Reputation: 3621
Looks to me like you need to add a file name to the end of the temp path. Try this:
StreamWriter downloadWriter =
new StreamWriter(Path.Combine(Path.GetTempPath(), fileName));
Upvotes: 5