Click here to Skip to main content
14,168,858 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

49K views
16 bookmarked
Posted 21 Sep 2013
Licenced CPOL

Creating Wifi HotSpot with the Help of CMD and C#

, 21 Sep 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Zain Ul Abidin
Pakistan Pakistan
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionwifi hotspot Pin
Member 1346369229-Nov-17 1:04
memberMember 1346369229-Nov-17 1:04 
QuestionI want to get connected users list with IPAddress, MAC and Device name Pin
Member 854409428-Jan-16 1:29
memberMember 854409428-Jan-16 1:29 
AnswerRe: I want to get connected users list with IPAddress, MAC and Device name Pin
Zain Ul Abidin18-Mar-16 1:26
memberZain Ul Abidin18-Mar-16 1:26 
NewsSource Code Pin
Manav_Rachna9-Nov-15 4:04
memberManav_Rachna9-Nov-15 4:04 
QuestionWifi hotspot sharing Pin
Pankaj Choudhury9-Jan-15 20:11
memberPankaj Choudhury9-Jan-15 20:11 
AnswerRe: Wifi hotspot sharing Pin
Zain Ul Abidin10-Jan-15 8:20
memberZain Ul Abidin10-Jan-15 8:20 
GeneralRe: Wifi hotspot sharing Pin
Sepehr Mohammad11-Mar-15 9:28
memberSepehr Mohammad11-Mar-15 9:28 
GeneralRe: Wifi hotspot sharing Pin
Zain Ul Abidin24-Mar-15 0:01
memberZain Ul Abidin24-Mar-15 0:01 
GeneralRe: Wifi hotspot sharing Pin
Sepehr Mohammad24-Mar-15 0:04
memberSepehr Mohammad24-Mar-15 0:04 
Questionsource code Pin
Member 1053376718-Nov-14 6:10
memberMember 1053376718-Nov-14 6:10 
AnswerRe: source code Pin
Zain Ul Abidin21-Nov-14 6:19
memberZain Ul Abidin21-Nov-14 6:19 
GeneralRe: source code Pin
Member 1053376712-Dec-14 3:41
memberMember 1053376712-Dec-14 3:41 
GeneralRe: source code Pin
Manav_Rachna18-Nov-15 20:21
memberManav_Rachna18-Nov-15 20:21 
GeneralRe: source code Pin
Zain Ul Abidin22-Nov-15 1:22
memberZain Ul Abidin22-Nov-15 1:22 
QuestionCan you tell me how to add admin rights. Pin
Member 86666691-Sep-14 21:35
memberMember 86666691-Sep-14 21:35 
AnswerRe: Can you tell me how to add admin rights. Pin
Zain Ul Abidin5-Sep-14 21:58
memberZain Ul Abidin5-Sep-14 21:58 
Generalthnkx Pin
Sikandar Ali23-Jan-14 3:34
memberSikandar Ali23-Jan-14 3:34 
QuestionAny way to check status of network? Pin
Oleg Evans27-Dec-13 5:46
memberOleg Evans27-Dec-13 5:46 
AnswerRe: Any way to check status of network? Pin
Zain Ul Abidin31-Dec-13 2:35
memberZain Ul Abidin31-Dec-13 2:35 
SuggestionNeed to add/ customize: Pin
bluetoothfx17-Dec-13 8:56
memberbluetoothfx17-Dec-13 8:56 
Questionerror Pin
Chuy951018-Nov-13 15:44
memberChuy951018-Nov-13 15:44 
AnswerRe: error Pin
Zain Ul Abidin19-Nov-13 9:19
memberZain Ul Abidin19-Nov-13 9:19 
QuestionIP Address never assigned Pin
John C Wollner29-Oct-13 1:39
memberJohn C Wollner29-Oct-13 1:39 
AnswerRe: IP Address never assigned Pin
Zain Ul Abidin30-Oct-13 4:36
memberZain Ul Abidin30-Oct-13 4:36 
GeneralMy vote of 3 Pin
Florian Rosmann22-Sep-13 5:46
memberFlorian Rosmann22-Sep-13 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190518.1 | Last Updated 22 Sep 2013
Article Copyright 2013 by Zain Ul Abidin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid