Change your ip and dns using netsh easily

Sometimes i am connecting different networks at home,school,work or a cafe and every connections are different and needed different  ip configuration some of static other dinamic . if you don’t like in this situation netsh can help you.

you can save save netsh consol command into a batch file and just run it when you needed to change your configuration.

for change ip

Usage:
netsh interface ip set address “Local Area Connection” staticipaddr subnetmask gateway metric

Example:
netsh interface ip set address “Local Area Connection” static 192.168.0.10 255.255.255.0 192.168.0.1 1
netsh interface ip set address “Local Area Connection” dhcp

Change dns example:
netsh interface ip set dns “Local Area Connection” static 192.168.0.200

Change dns as using dhcp
netsh interface ip set dns “Local Area Connection” dhcp

That’s it…

Advertisements

Some way to modify rendered output html

Another way to use a global variable in your page as overriding render method of your form. Ok its not a good and suggested way and also have performance problems but strangely it will work sometimes and also provide writing less code in html side.

Our form’s html view:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebForm1.aspx.cs” Inherits=”MultiLangTest.WebForm1″ %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>

$$GlobalVariable

<a href=”http://www.google.com” >$$GlobalVariable</a>

<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>

$$GlobalVariable

<!–
<asp:Label ID = “Label1″  runat=”server”
Text =”<%= You know Its not working with asp:x tags
and also not necessery but sometimes strangely
you want to use this %>” />

than You can do it like this:
–>
<asp:Label ID = “Hello”  runat=”server” Text =”$$GlobalVariable” />

</div>
</form>
</body>
</html>

Our form’s codebehind:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            //we will create a memory stream for holding data
            using (MemoryStream ms = new MemoryStream())
            {
                //A streamwriter to write memory
                using (StreamWriter sw = new StreamWriter(ms))
                {
                    //this is our html writer.
                    HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
                    //Complete other renders
                    base.Render(htmlWriter);

                    htmlWriter.Flush();

                    //Set memory stream to start position.
                    ms.Position = 0;

                    //read the data
                    using (System.IO.StreamReader _Reader = new System.IO.StreamReader(ms))
                    {
                        string txt = _Reader.ReadToEnd();
                        //replace global variable
                        txt = txt.Replace("$$GlobalVariable", "Hello World" );
                        Response.Write(txt);
                        _Reader.Close();
                    }
                }
            }
        }
    }

Here is the output:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head><title>

</title></head>
<body>
<form name=”form1″ method=”post” action=”WebForm1.aspx” id=”form1″>
<div>
<input type=”hidden” name=”__VIEWSTATE” id=”__VIEWSTATE” value=”/wEPDwUKMTg3NjI4NzkzNmRkxS9N0UwBHiMGrNo9vYAKQmdAfAc=” />
</div>

<div>

Hello World

<a href=”http://www.google.com” >Hello World</a>

<div title=”Hello World” > </div>
<div title=”Hello World” > </div>
<div title=”Hello World” > </div>
<div title=”Hello World” > </div>
<div title=”Hello World” > </div>

Hello World

<!–
<span id=”Label1″><%= You know Its not working with asp:x tage
and also not necessery but sometimes strangely
you want to use this %></span>

than You can do it like this:
–>
<span id=”Hello”>Hello World</span>

</div>
</form>
</body>
</html>

c# Ping with Simple Pinger Example

using System;
using System.Threading;
using System.Windows.Forms;
using System.Net.NetworkInformation;
 
 
namespace ContextHelper
{
    static class Pinger
    {
        public static void Ping(string url_or_IP)
        {
            using (Ping ping = new Ping())
            {
 
                try
                {
                    Console.Write("    {0}...", url_or_IP);
                    PingReply reply = ping.Send(url_or_IP, 100);
                    if (reply.Status == IPStatus.Success)
                    {
                        Console.WriteLine("Success - IP Address:{0}", reply.Address, reply.RoundtripTime);
                    }
                    else
                    {
                        Console.WriteLine(reply.Status);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error ({0})",
                        ex.InnerException.Message);
                }
 
            }
        }
    }
}

Asp.net c# save scroll position when postback

this is the mana:
Page.MaintainScrollPositionOnPostBack Property

Msdn say  “gets or sets a value indicating whether to return the user to the same position in the client browser after postback. This property replaces the obsolete SmartNavigation property.When Web pages are posted back to the server, the user is returned to the top of the page. On long Web pages, this means that the user has to scroll the page back to the last position on the page.When the MaintainScrollPositionOnPostback() property is set to true, the user is instead returned to the last position on the page.”

Usage:
<asp:Page MaintainScrollPositionOnPostBack=”True|False” />

Or  you can add your web config like this for all pages
<system.web>
<pages validateRequest=”true”  maintainScrollPositionOnPostBack=”true”>
</system.web>

Important Note: There is also a problem with this . Does not work with other browsers except ie.

Source:
Happy codding !!

c# Single instance of an application on one machine

Some times you want one running instance for your application then you can deny another running instance with this code.

using System;
using System.Threading;
using System.Windows.Forms;
 
 
namespace MyApp
{
    static class Program
    {
        static Mutex _Mutex = null;
 
        [STAThread]
        static void Main()
        {
            bool myAppFirstInstance;
            _Mutex = new Mutex(true, "Global\\MyApp ", out myAppFirstInstance);
            if (myAppFirstInstance)
                Application.Run(new MainForm());
            else
                MessageBox.Show("There is already exists a running instance of this application",
                    "Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }
    }
}

c# Use As , not is in

In C#, use as, not is. The is keyword is used to see whether a reference can be cast as particular type, but it doesn’t return a reference converted to that type. So usually, if you get a positive result from the is, the first thing you’ll do is a cast—effectively implementing the same cast twice. With the as keyword, a reference cast as the new type is returned if it’s valid and a null is returned if it’s not. You then can check for the null and do what you like. The asapproach is fully 50% faster than the is approach.

c# Excel response with httphandler

An example of excel output used by HttpHandler

public class IExcelHandler : IHttpHandler
    {
 
        #region IHttpHandler Members
 
        public bool IsReusable
        {
            get { return false; }
        }
 
        public void ProcessRequest(HttpContext context)
        {
            // set Content type for Excel
            context.Response.ContentType = "application/vnd.ms-excel";
            // for download and give a file name
            context.Response.AppendHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
            context.Response.Charset = "";
 
            //Write here your content
            // context.Response.Write(data);
 
            context.Response.End();
        }
 
        #endregion
    }
...