Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a batch file through c#. It works in local but when I deployed it in IIS it is not working.

What I have tried:

My code:

C#
StreamWriter sw = File.CreateText("C:\\inetpub\\wwwroot\\StarRECONPADSS\\Files\\" + "TESTDEL.bat");
sw.WriteLine("set _p= %-p%");
sw.WriteLine("sdelete _p 7 " + sourcePath + FileName);
sw.Close();


Process p = new Process();
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "C:\\inetpub\\wwwroot\\StarRECONPADSS\\Files\\" + "TESTDEL.bat";
Write(p.StartInfo.FileName);
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
Write(p.StartInfo.FileName);
p.StartInfo.RedirectStandardError = true;
Write("BatchFile: Start() Execution Started");
p.Start();
p.WaitForExit();
Write("BatchFile: WaitForExit() Execution Ended");
p.Dispose();
Posted
Updated 24-Jul-19 6:08am
v2
Comments
F-ES Sitecore 24-Jul-19 4:58am    
"Doesn't work" doesn't give anyone enough information to help you. Check the process is actually launching by monitoring the task list. It could be an issue with whatever the bat file does, maybe it needs access to the desktop but can't, maybe it needs security access to something but it doesn't have it. As a side note, if you are intending this bat file to run on the client then that's not possible.
Dave Kreskowiak 24-Jul-19 9:22am    
Why are you using a batch file to delete files? Why do you feel the need to use Secure Delete on a web server?

Next, that batch file look ... funky. Are you sure it even works after it's been written?
ZurdoDev 24-Jul-19 15:14pm    
So debug it. What do you want us to do?

1 solution

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:
C#
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900