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.

2
  • Is the document stored in IIS working directory or in a more traditional such as network drive? Commented Jan 28, 2019 at 3:16
  • @mr.coffee thanks for ur reply. it is stored in IIS Working directory. Commented Jan 28, 2019 at 3:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.