Loading from a Text File with Separators

To load users, you are going to need a text file with separators, where the values of each column are separated from each other with ";". The information encoding in the file corresponds to the code page 1251. One line in the file stands for one uploaded user. The first column will contains the room number, the second - the first, middle and last names separated with spaces and the third - the phone number

Take into account that the number of uploaded users should not be greater than the number of user licenses.

Example of a line from a file.

100; Anne Joan Carter; 98-378-90-90

For the script to work correctly, connect the assemblies:

 

using System.IO;

using EleWise.ELMA.Security.Managers;<span style="font-family: Tahoma;"> </span>

Script:

// specify the file and the encoding type

string filename = @"D:\users.csv";

Encoding enc    = Encoding.GetEncoding(1251);

 

// open the stream for displaying the file characters in a specific encoding

StreamReader rs     = new StreamReader(filename,  enc);

 

// buffer string

string stline   =  "";

 

//  start the cycle of reading strings from the stream

while ((stline = rs.ReadLine()) != null)   

{

    // generate an array of the file fields (room number, full name, phone number)

    string[] stparts    = stline.Split(’;’);

 

    // parse the full name field value to Name (stfio[0]), Middle Name (stfio[1]), Last name

    // (stfio[2])

 

    string[] stfio  = stparts[2].Split(’ ’);

 

    //  create an instance of the User object

    var UserNew     = UserManager.Instance.Create();

    UserNew.FirstName   = stfio[1]; //   Name
    UserNew.MiddleName= stfio[2];   //   Middle Name
    UserNew.LastName    = stfio[0]; //   Last Name
 

    // create an account (login) for signing in to the system (miller)

    UserNew.UserName    = stfio[0] + stfio[1].Substring(0,1) +stfio[2].Substring(0,1);       

    UserNew.FullName    = stparts[2];    // full name
    UserNew.RoomNumber = stparts[0];    // room number 

    UserNew.WorkPhone  = stparts[3];    // phone number 

 

    // save the object instance

    UserNew.Save();

}

//   close the stream
rs.Close();