« Previous Next »

Thread: IIS 5.1 skips ASP.NET Code

Last post 04-02-2009 7:09 AM by Krushna Chandra Sahu. 3 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (4 items)

Sort Posts:

  • 06-08-2006, 2:16 AM

    IIS 5.1 skips ASP.NET Code

    OK, I'm trying to write a script to grab the MAC Address of a client using the clients IP.  Here's the code that I got from developersdex.com written by Armoghan Asif:
    CODE START

    public string IP2Mac(string IP )

    {

    string result = "Not found";

    try

    {

    ProcessStartInfo psi = new ProcessStartInfo("nbtstat", "-A "+ IP);

    Process proc = new Process();

    psi.RedirectStandardInput = true;

    psi.RedirectStandardOutput = true;

    psi.RedirectStandardError = true;

    psi.CreateNoWindow= true;

    psi.UseShellExecute = false;

    proc.StartInfo = psi;

    proc.Start();

    proc.WaitForExit();

    StreamReader sr = proc.StandardOutput;

    StreamReader err = proc.StandardError;

    string str = "";

    bool looking = true;

    while (looking )

    {

    str = sr.ReadLine().Trim().ToLower ();

    int i = str.IndexOf("mac address = ");

    if(str == null )

    looking = false;

    if ( i != -1)

    {

    result = str.Substring (i + "mac address = ".Length );

    looking = false;

    }

    }

    }

    catch (Exception err)

    {

    Console.Error.WriteLine(err.Message);

    }

    return result;

    }

    CODE END

    I pass in the IP from a remote client.  I'm coding with Visual Web Developer 2005 express edition.  Using the web server with VWD 2005 EE, it works great.  But using IIS 5.1, the MAC returns NOT FOUND.  I placed Response.Write("Some text") before the while loop and after the while loops closing curly brace.  I also added a counter variable that I print to the screen during the while loops execution.  The Response.Write("Some text") that is placed before the while loop gets executed.  None of the other ones do in IIS 5.1.  So when running the code in IIS 5.1 (this is on Windows XP SP2), it goes into the function, starts the processes but totally skips the while loop and the Response.Write code after the closing curly brace of the while loop.  Any ideas why this would happen? 

  • 06-08-2006, 3:44 PM In reply to

    Re: IIS 5.1 skips ASP.NET Code

    Part of the problem is that I was using console.writeline instead of response.write.  So now IIS tells me "Object reference not set to an instance of an object".  This is only in IIS 5.1 and not the web server that comes with Visual Web Developer.  The Visual Web Developer web server runs properly.  The problem code is

    while (looking)
                    {
                        Response.Write("DOING SOMETHING<br />");
                        str = sr.ReadLine();
                        Response.Write("ONCE AGAIN, DOING SOMETHING ELSE<br />");
                        int i = str.IndexOf("MAC Address = ");
                        Response.Write("Just used Index of Method (or property)<br />");
                        if (str == null)
                        {
                            looking = false;
                        }
                        if (i != -1)
                        {
                            result = str.Substring(i + "mac address = ".Length);
                            looking = false;
                        }
                        count++;
                        Response.Write(count.ToString() + "<br />");
                    }

    The line Response.Write("Just used Index of Method (or property)<br />"); never gets ran.  Any ideas?
  • 06-08-2006, 3:56 PM In reply to

    • thomad
    • Top 25 Contributor
    • Joined on 08-20-2002, 3:28 PM
    • Redmond
    • Posts 503

    Re: IIS 5.1 skips ASP.NET Code

    DeeJay,

    I only have W2K3 here: Can you try this?

    <%@Language="C#" ValidateRequest="false"%>
    <html>
    <body>
    <h1>Output Redirect</h1><p>
    <form id="form1" runat="Server">
    Command: <asp:TextBox id=tbComm runat="Server" />
    <asp:Button id="btn1" Text="Send Command" onclick="Button_Click" runat="Server"/><br><hr>
    Output: <asp:TextBox  id=textBox1 runat="Server" width="500" height="300"/>

    </form>
    <script runat="Server">
     
    private void Button_Click(object sender, System.EventArgs e)
    {
       System.Diagnostics.Process p = new System.Diagnostics.Process();
       System.IO.StreamWriter sw;
       System.IO.StreamReader sr;
       System.IO.StreamReader err;
       textBox1.Text = "";
       textBox1.TextMode = TextBoxMode.MultiLine;

    //textBox1.Wordwrap= true;


       System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo("cmd");
       psI.UseShellExecute = false;
       psI.RedirectStandardInput = true;
       psI.RedirectStandardOutput = true;
       psI.RedirectStandardError = true;
       psI.CreateNoWindow = true;
       p.StartInfo = psI;
      
       p.Start();
       sw = p.StandardInput;
       sr = p.StandardOutput;
       err = p.StandardError;

       sw.AutoFlush = true;
       if (tbComm.Text != "")
        sw.WriteLine(tbComm.Text);
       else
        //execute default command
        sw.WriteLine("dir \\");
      
       sw.Close();

       textBox1.Text = sr.ReadToEnd();
       textBox1.Text += err.ReadToEnd();
    }
    </script>
    </body>
    </html>

    Hope this helps.

    Thomas Deml
    Program Manager
    Internet Information Services
    Microsoft Corp.
  • 04-02-2009, 7:09 AM In reply to

    Re: IIS 5.1 skips ASP.NET Code

    thomad:

    DeeJay,

    I only have W2K3 here: Can you try this?

    <%@Language="C#" ValidateRequest="false"%>
    <html>
    <body>
    <h1>Output Redirect</h1><p>
    <form id="form1" runat="Server">
    Command: <asp:TextBox id=tbComm runat="Server" />
    <asp:Button id="btn1" Text="Send Command" onclick="Button_Click" runat="Server"/><br><hr>
    Output: <asp:TextBox  id=textBox1 runat="Server" width="500" height="300"/>

    </form>
    <script runat="Server">
     
    private void Button_Click(object sender, System.EventArgs e)
    {
       System.Diagnostics.Process p = new System.Diagnostics.Process();
       System.IO.StreamWriter sw;
       System.IO.StreamReader sr;
       System.IO.StreamReader err;
       textBox1.Text = "";
       textBox1.TextMode = TextBoxMode.MultiLine;

    //textBox1.Wordwrap= true;


       System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo("cmd");
       psI.UseShellExecute = false;
       psI.RedirectStandardInput = true;
       psI.RedirectStandardOutput = true;
       psI.RedirectStandardError = true;
       psI.CreateNoWindow = true;
       p.StartInfo = psI;
      
       p.Start();
       sw = p.StandardInput;
       sr = p.StandardOutput;
       err = p.StandardError;

       sw.AutoFlush = true;
       if (tbComm.Text != "")
        sw.WriteLine(tbComm.Text);
       else
        //execute default command
        sw.WriteLine("dir \\");
      
       sw.Close();

       textBox1.Text = sr.ReadToEnd();
       textBox1.Text += err.ReadToEnd();
    }
    </script>
    </body>
    </html>

    Hope this helps.

     

     

    When I run this code it works fine from the application.

    But when I put in IIS creating a virtual directory, it shows the following error.

    Server Error in '/PrintToPDF' Application.

    Access is denied

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ComponentModel.Win32Exception: Access is denied

    Source Error:

    Line 137:        p.StartInfo = psI;
    Line 138:
    Line 139: p.Start();
    Line 140: sw = p.StandardInput;
    Line 141: sr = p.StandardOutput;

    Source File: e:\Krushna\PrintToPDF\DocToPdf.aspx.cs    Line: 139

    Stack Trace:

    [Win32Exception (0x80004005): Access is denied]
    System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) +2097
    System.Diagnostics.Process.Start() +140
    DocToPdf.Button1_Click(Object sender, EventArgs e) in e:\Krushna\PrintToPDF\DocToPdf.aspx.cs:139
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 

     

     

    So, Please let me know what modifications I need to do to work this fine.

     Thank you.

     One more thing,

    I've an application developed in ASP.Net to convert any file to pdf file using Adobe PDF Printer.
    In the .aspx page I've kept one button control and in the click event I've written :

    protected void Button1_Click(object sender, EventArgs e)
    {
    try
                {
                    Process printProcess = new Process();

                    printProcess.StartInfo.FileName = "E:\\Krushna.doc";

                    printProcess.StartInfo.Verb = "printto";
                    printProcess.StartInfo.Arguments = "\"Adobe PDF\"";

                    printProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
                    printProcess.StartInfo.CreateNoWindow = true;
                    printProcess.Start();

                    try
                    {
                        //printProcess.WaitForExit();
                    }
                    catch (InvalidOperationException ex)
                    {
                        throw ex;
                    }

                    printProcess.Dispose();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    }

    Please note that I'm using Adobe Acrobat 6.0 Professional and PDF Printer is installed in my system and it is set to default printer.
    In the properties of the PDF Printer, in the Port tab, I've set the port to "My Document\*.pdf " . It will save the .pdf file to My Document.

    I'm using ASP.Net 2.0 with IIS 5.1 installed.

    The issue is :
    This is working fine while running the application directly(i.e. the Krushna.doc file is converting to Krushna.pdf and save to My Document Folder) .

    But when the application running from IIS, it is asking to save the pdf file i.e. one save dialog window is opened. But I need to directly save the pdf file to the My Document Folder.

    How I'll do it so that the pdf file will save automatically when running from IIS ?

    Please reply...It is urgent requirment.

    Thank you.

Page 1 of 1 (4 items)
Microsoft Communities