This is my current Code. But I think the problem was in the IIS because I try this in debug mode and it works, and when I published the bat file won't open.
public ActionResult Print(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Item item = db.Items.Find(id);
Origin origin = db.Origins.Where(x => x.OriginMainID == item.OriginMainID && x.IsActive == true).FirstOrDefault();
var user = UserManager.FindById(item.CreatedBy);
string fileLoc = @"e:\SAMPLE.txt";
string text = "";
text += item.PropertyCode + "," + origin.Assignee.LName + " " + origin.Assignee.FName + "," + origin.Assignee.Department.ShortCode + "," + user.LastName + " " + user.FirstName + "," + origin.Assignee.CMID;
if (System.IO.File.Exists(fileLoc))
{
using (StreamWriter sw = new StreamWriter(fileLoc))
{
sw.Write(text);
}
}
if (item == null && origin == null)
{
return HttpNotFound();
}
Process.Start(@"C:\PANDA.bat");
return RedirectToAction("Index");
}
Instead of writing Process.Start(@"C:\\PANDA.bat");
Try using the following snippet.
Process proc = new Process();
proc.StartInfo.FileName = @"C:\PANDA.bat";
proc.StartInfo.Verb = "runas";
proc.Start();
proc.StartInfo.Verb = "runas"; This line will execute the code as Administrator on Hosted environment, So it will work on IIS.
Your IIS will work under a special user either the IIS_USRS group or under a specific account used under impersonation. In this case, you need to do following:-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.