Are you absolutely sure that the path
"C:\\inetpub\\wwwroot\\StarRECONPADSS\\Files\\" + "TESTDEL.bat"
actually exists
on the server? IIS' default root path can be changed, for example to put it on a separate drive. If the
folder hierarchy does not exist on the server, it will not be automatically created by the
File.CreateText()
method.
So first, check that the folder
C:\inetpub\wwwroot\StarRECONPADSS\Files
actually exists on the server, and that IIS' service account has access rights to it.
Moreover, since
StreamWriter
does implement
IDisposable
interface, you better use it in an
using
block; like this:
using (StreamWriter sw = File.CreateText("C:\\inetpub\\wwwroot\\StarRECONPADSS\\Files\\" + "TESTDEL.bat"))
{
sw.WriteLine("set _p= %-p%");
sw.WriteLine("sdelete _p 7 " + sourcePath + FileName);
}
Streamwriter will be automatically disposed at the end of the
using
block, and disposing it wipes the need to close it explicitly.
As a side note, it is generally a bad idea to hard-code paths like that in your code files. This usually leads to the kind of issues you are dealing with. Not being an ASP.NET specialist, I cannot provide a specialized recommandations on how to better architecture application's access paths; but some here may have valuable advices to provide.