1

Batch file properties

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");
    }
Melebius
  • 6,183
  • 4
  • 39
  • 52

2 Answers2

0

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.

Karan
  • 12,059
  • 3
  • 24
  • 40
  • Thank you sir for your nice comment and explanation. I tried your code but it didn't work. – AL THEO LOUIS Apr 25 '18 at 06:41
  • In debug mode, it's working, but when I published in IIS it didn't work – AL THEO LOUIS Apr 25 '18 at 06:45
  • 1
    `runas` doesn't magically make things run as an administrator. If the current user account isn't an administrator or is an administrator running with a restricted token, then UAC prompts need to be displayed - but IIS typically runs on a machine with nobody logged in to *respond* to a UAC prompt. – Damien_The_Unbeliever Apr 25 '18 at 06:54
0

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:-

  1. File access to be from a directory and never from root of drive. Only administrator can access a file from root. Probably your user account is admin on your machine so you are not noticing this while running via VS
  2. Set a specific account for your web app, and give execute permissions for that account on the folder containing the batch file. Then in your web.config, setup impersonation and provide that user's account name.
NitinSingh
  • 2,029
  • 1
  • 15
  • 33