I am trying to write a routine to upload a file through a web service to our
server. I've enclosed the following code and when I run it I get an error:
Exception Occurred. Error: Logon failure: unknown user name or bad
password.\n Trying to write to file I:\\ECS\\Content\\UploadFile.txt Last
Action = File Open
Does this mean that the ASP.Net account (which I haven't changed) doesn't
have rights to the directory or that there is something actually wrong with
the account. Other things work OK - just not this.
The actual code is:
public bool UploadContentFile(string pvsFileName, Byte[] pvobjFile, ref
string prsReturn)
{
bool lbReturnStatus = false;
string lsBaseDirectory =
ConfigurationSettings.AppSettings["ContentDir"].ToString();
FileStream lobjFS = null;
// making sure the base directory string ends with a \ so that
// the file is uploaded to the correct spot
if ( !lsBaseDirectory.EndsWith(@"\") )
lsBaseDirectory += @"\";
string lsFullFileName = lsBaseDirectory + pvsFileName;
string lsLastAction = "";
try
{
// creates the file by opening and setting the fileaccess to write
lsLastAction = "File Open";
lobjFS = File.Open(lsFullFileName, FileMode.Create, FileAccess.Write);
// byte count
long llLen = pvobjFile.Length;
// Write the file
lsLastAction = "File Write";
lobjFS.Write(pvobjFile, 0, Convert.ToInt32(llLen));
// clears the buffer and writes any buffered data
lsLastAction = "File Flush";
lobjFS.Flush();
// close the file so it can be access by other files
lsLastAction = "File Close";
lobjFS.Close();
// return a successful upload status
lbReturnStatus = true;
}
catch ( System.UnauthorizedAccessException exc )
{
// Account does not have enough access to create the file, do to either
login or system permissions
prsReturn = "Unauthorized Access Exception Occurred. Error: " +
exc.Message.ToString();
}
catch ( Exception exc )
{
// catchall error handler
prsReturn = "Exception Occurred. Error: " + exc.Message.ToString() + "
Trying to write to file " + lsFullFileName +
" Last Action = " + lsLastAction;
}
finally
{
// cleanup just in case
if ( lobjFS != null )
{
lobjFS.Close();
}
}
return lbReturnStatus;
}
TIA - Jeff.