Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Wifi HotSpot with the Help of CMD and C#

0.00/5 (No votes)
21 Sep 2013 4  
How to create a Wifi HotSpot with the help of CMD and C#

Introduction

There are many applications on the internet that can convert your laptop or computer into Wifi hotspot like Conectify. This is what this application will do but no advance features like sharing internet. However Creation is very simple just specify the SSID and a key and you are done!

How It Works

The basic principal is that the actual creation is hidden behind the DOS command that we have to issue this application is the front end (GUI version) of this irritating command issuing procedure. so without getting into mess of directly accessing the hardware and services, we can achieve our purpose.

Background

The basic background of DOS is good and also of basic knowledge of C# to implement the code.

Using the Code

Assuming you have created the project and in Windows Form there are two text boxes, two labels and a button, copy and paste the following code in the Click event handler of Button:

string ssid = textBox1.Text, key = textBox2.Text;
            if (!connect)
            {
                if (textBox1.Text == null || textBox1.Text == "")
                {
                    MessageBox.Show("SSID cannot be left blank !", 
                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    if (textBox2.Text == null || textBox2.Text == "")
                    {
                        MessageBox.Show("Key value cannot be left blank !", 
                        "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        if (key.Length >= 6)
                        {
                            Zedfi_Hotspot(ssid, key,true);
                            textBox1.Enabled = false;
                            textBox2.Enabled = false;
                            button1.Text = "Stop";
                            connect = true;
                        }
                        else
                        {
                            MessageBox.Show("Key should be more then or Equal to 6 Characters !", 
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            else
            {
                Zedfi_Hotspot(null, null, false);
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                button1.Text = "Start";
                connect = false;
            }

Following is the method. Just copy it in your project to use it.

 private void Zedfi_Hotspot(string ssid, string key,bool status)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.CreateNoWindow = true;
            processStartInfo.UseShellExecute = false;
            Process process = Process.Start(processStartInfo);

            if (process != null)
            {
                if (status)
                {
                    process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow 
                    ssid=" + ssid + " key=" + key);
                    process.StandardInput.WriteLine("netsh wlan start hosted network");
                    process.StandardInput.Close();
                }
                else
                {
                    process.StandardInput.WriteLine("netsh wlan stop hostednetwork");
                    process.StandardInput.Close();
                }
            }
        }

One thing to remember these DOS commands work only if the application has admin privileges. If the application has no privileges, then the hotspot will not be created so to solve this problem, we have to check whether we have privileges or not. The following method is for this purpose:

 public static bool IsAdmin()
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal p = new WindowsPrincipal(id);
            return p.IsInRole(WindowsBuiltInRole.Administrator);
        }

This method will return false or true accordingly. If this returned false then it means we have no privileges so we have to restart the application in admin mode to restart. The following code must be copied in your project

 public void RestartElevated()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.CreateNoWindow = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
            startInfo.Verb = "runas";
            try
            {
                Process p = Process.Start(startInfo);
            }
            catch
            {

            }

            System.Windows.Forms.Application.Exit();
        }

Now the last step is to create the form Load event where we actually want to check the privileges and do something accordingly copy the following code into form Load event

if (!IsAdmin())
            {
                RestartElevated();
            }

Also it is desired that if we close the application, then the Hotspot should also be stopped for this. Just copy the following code into the form Closing event:

Zedfi_Hotspot(null,null,false);
            Application.Exit();

Now all is done. Just build the project and enjoy!

Points of Interest

This hotspot created can be useful to create a network and for data sharing among the network but internet sharing is not applicable till now. Any suggestions will be welcome!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here