0

I am trying to print Excel(.xlsx) file via my web application by clicking a button. Everything works fine on my local, however when I upload to IIS, the moment I click the button. The page will just keep loading.

                string destinationFileWord = filePath;

                Process print = new Process();
                print.StartInfo.FileName = destinationFileWord;
                print.StartInfo.UseShellExecute = true;

                print.StartInfo.CreateNoWindow = true;
                print.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                print.StartInfo.Verb = "PrintTo";

                print.StartInfo.Arguments = Utility.GetAppSettings("PrinterSettings", "Address1");
                print.StartInfo.WorkingDirectory = Path.GetDirectoryName(destinationFileWord);
                print.Start();

                if (print.HasExited == false)
                {
                    print.WaitForExit(5000);

                }

Does anyone know what might be the problem?

Thank you.

Sarah
  • 329
  • 5
  • 21

1 Answers1

1

Your issue is mostly likely the directory mapping. On the server you need to use something like this to get the current working directory.

HttpContext.Current.Server.MapPath("~/") 

There are a number of other methods posted in this SO post:

How do I get the current directory in a web service

EDIT

For .NET Core you need to use IHostingEnvironment.ContentRootPath:

How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac

mr.coffee
  • 962
  • 8
  • 22
  • Hi Thanks for your reply. But I dont think thats the problem, because I actually had another function to print PDF and it works. What I am trying to say is that there are no problem with finding the filePath or fileName. – Sarah Jan 28 '19 at 03:22
  • HI. Turns out it was because of Microsoft Excel, if I change the default app to open .XLSX to LibreOffice then it works. Any idea what might be the probleM – Sarah Jan 28 '19 at 04:45