[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Архив - только для чтения
Форум » Программирование » С#/C++ » Windows Forms » C# Tutorial - Login with .ini fil
C# Tutorial - Login with .ini fil
nuzesvenДата: Пятница, 16.01.2015, 01:05 | Сообщение # 1
Генералиссимус
Группа: Администраторы
Сообщений: 6
Награды: 2
Репутация: 228
Статус: Offline
Hello everybody its being a while since i posted some C# tutorial,soo i had a bit of time and i did one today.In this lesson i will be showing you how to make Login board with saving your informations thanks to .ini file system.Ready Lets begin

Create New project call it "Login ini" after that you need to add two new textboxes and one button call your button "Login".After you did all of this you will need to add one new .cs file name it Ini.cs it will be configuration file for writing our ini files i included file as attachment.then double click on your button.Now you have entered "code designer" at top of source add "usin Ini;" soo we add our configuration file to Form1.Now inside
Код
private void button1_Click(object sender, EventArgs e){}

You will need to add
Код
if (textBox1.Text == "Admin" && textBox2.Text == "123123")
             {
                 //Suscesfull Login Message
                 MessageBox.Show("Login Suscesfull,Welcome Admin");
             }

This are correct login informations if correct infos are inputed it will say Login suscesfull we can also add else statement soo if inputed infos are wrong it will say
Код
else
             {
                 //Error Messagebox bad login
                 MessageBox.Show("Please check your login informations");
             }

OK now we have finished login part now only thing left to do is writing ini.We are still inside button1 function what you want to add now is
Код
        IniFile ini = new IniFile("C:\\test.ini");
             ini.IniWriteValue("C:\\", "Username", textBox1.Text);
             ini.IniWriteValue("C:\\", "Password", textBox2.Text);


Soo when button1 is pressed it will write test.ini file on "c:\\".now go back to your design editor and double click on whole form.Now we will inside
Код
private void Form1_Load(object sender, EventArgs e)
{}

Код
      IniFile ini = new IniFile("C:\\test.ini");
             ini.IniReadValue("C:\\", "Username");//textbox1
             ini.IniReadValue("C:\\", "Password");//textbox2

Soo all informations from test.ini file are loaded when form1 is opened this function is helpful as remember me checkbox but instead of checking and unchecking button it creates file on your harddisk.

This concludes the lesson.

Soon more tutorials,Blooder/Zeed.

P.S i couldnt upload file soo i will just post config file in other post

[Auto Merged]

Ini.cs configuration file:
Код
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
     /// <summary>

     /// Create a New INI file to store or load data

     /// </summary>

     public class IniFile
     {
         public string path;

         [DllImport("kernel32")]
         private static extern long WritePrivateProfileString(string section,
             string key, string val, string filePath);
         [DllImport("kernel32")]
         private static extern int GetPrivateProfileString(string section,
                  string key, string def, StringBuilder retVal,
             int size, string filePath);

         /// <summary>

         /// INIFile Constructor.

         /// </summary>

         /// <PARAM name="INIPath"></PARAM>

         public IniFile(string INIPath)
         {
             path = INIPath;
         }
         /// <summary>

         /// Write Data to the INI File

         /// </summary>

         /// <PARAM name="Section"></PARAM>

         /// Section name

         /// <PARAM name="Key"></PARAM>

         /// Key Name

         /// <PARAM name="Value"></PARAM>

         /// Value Name

         public void IniWriteValue(string Section, string Key, string Value)
         {
             WritePrivateProfileString(Section, Key, Value, this.path);
         }

         /// <summary>

         /// Read Data Value From the Ini File

         /// </summary>

         /// <PARAM name="Section"></PARAM>

         /// <PARAM name="Key"></PARAM>

         /// <PARAM name="Path"></PARAM>

         /// <returns></returns>

         public string IniReadValue(string Section, string Key)
         {
             StringBuilder temp = new StringBuilder(255);
             int i = GetPrivateProfileString(Section, Key, "", temp,
                    255, this.path);
             return temp.ToString();

         }
     }
}

Credits:Zeed


 
Форум » Программирование » С#/C++ » Windows Forms » C# Tutorial - Login with .ini fil
  • Страница 1 из 1
  • 1
Поиск: