Wednesday, 1 May 2013

User Defined HTTP SERVER in C#

User Defined HTTP SERVER in C#

using System;
using System.Linq;
using System.Net;
using System.Threading;
namespace ConsoleApplication3
{
    class Program
    {
        public static HttpListener _list = new HttpListener();
        static void Main(string[] args)
        {   
                       _list.Prefixes.Add("http://localhost:2000/");
                     _list.Start()
            Console.WriteLine("server started");
            Thread th = new Thread(loadsite);
            th.Start();
            Console.Read();
        }
        protected static void loadsite()
        {
            while (true)
            {
                HttpListenerContext context = _list.GetContext();
                byte[] response = Encoding.UTF8.GetBytes("<html><body>hello world</body></html>");
                context.Response.OutputStream.Write(response, 0, response.Length);
                context.Response.KeepAlive = false;
                context.Response.Close();

        }
    }
}

Upload and Extract file in Asp.net C3#

Upload and Extract file in ASP.NET C#


.aspx

<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload and Extract"
            onclick="btnUpload_Click" />
        <br />
        <asp:Label ID="lblMsg" runat="server"></asp:Label>
    </div>


.cs

 if (FileUpload1.HasFile)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.PostedFile.SaveAs(Server.MapPath("compressed") + "/" + FileName);

            FileInfo finfo = new FileInfo(Server.MapPath("compressed") + "/" + FileName);
            long FileInBytes = finfo.Length;
            long FileInKB = finfo.Length / 1024;
            FastZip archiveFile = new FastZip();
            archiveFile.ExtractZip(Server.MapPath("compressed") + "/" + FileName, Server.MapPath("extracted"), "");
            lblMsg.Text = "File uploaded and extracted successfully,,<br />";
            lblMsg.Text += "File Name: " + FileName + "<br />";
            lblMsg.Text += "Size:" + FileInKB.ToString() + "KB <br />";
        }