简体   繁体   中英

How to prompt to open a file in c# asp.net mvc

I have been trying to implement something like this, but it will time out on the wait for exit?

My objective is to open the file on the client's machine in notepad. The below code is what i originally had, but just learned taht this will only work on the server ,not the client.

    public JsonResult Index()
    {
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.EnableRaisingEvents = false;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.FileName = "notepad";

        proc.StartInfo.Arguments =  @"\\share\test.XML";
        proc.Start();
        proc.WaitForExit();
    }

Is there a way to do this?

All i am trying to do is open the test.xml file on a client's machine.

The code you have there will execute on the server side; not on the client-side. You can't open a file (or execute a program for that matter) on the client machine, from a browser. That would be a major security issue if a browser could do that.

Best thing you can do is either create a hyperlink to the file in the format file:///drive:/file.xml

Your code will start the notepad application and open the test.xml file in correctly

The notepad application will left open till it is close because you are using the WaitForExit method.

The WaitForExit method instructs the Process component to wait indefinitely for the associated process to exit.

Do you really want to use the WaitForExit in your MVC action

Please read the Remarks taken from MSDN for WaitForExit

The WaitForExit() overload is used to make the current thread wait until the associated process terminates. This method instructs the Process component to wait an infinite amount of time for the process and event handlers to exit. This can cause an application to stop responding. For example, if you call CloseMainWindow for a process that has a user interface, the request to the operating system to terminate the associated process might not be handled if the process is written to never enter its message loop.

Note: In the .NET Framework version 3.5 and earlier versions, the WaitForExit() overload waited for MaxValue milliseconds (approximately 24 days), not indefinitely. Also, previous versions did not wait for the event handlers to exit if the full MaxValue time was reached.

my weight the same and so I resolved using this

   using System.Xml;

   XmlTextReader reader = new XmlTextReader(
   Server.MapPath("mycompany.xml"));

    reader.WhitespaceHandling = WhitespaceHandling.None;
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(reader);
    reader.Close();        
    lbNodes.Items.Add("XML Document");
    XmlNode xnod = xmlDoc.DocumentElement;
    AddWithChildren(xnod, 1);

the complete example you can see here http://www.codeproject.com/Articles/4902/Reading-an-XML-file-using-NET

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM