PDA

View Full Version : Few functions



Coolguy
08-14-2015, 01:59 PM
Listbox threading for real-time log:



Thread thread1;

private void AddToLog(string message)
{
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.Items.Add(message)));
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.SelectedIndex = listBox1.Items.Count - 1));
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.SelectedIndex = -1));

}

//on start button

thread1 = new Thread(new ThreadStart(//your main function));
thread1.Start();

//on closing event

thread1.Abort();


Usage:


AddToLog("hi world");

Check if data folder/any text files exist and if they don't create them (example uses 1 text file):



public void createFolderAndTextFile()
{
string path = System.AppDomain.CurrentDomain.BaseDirectory;
[Only registered and activated users can see links]
string fileName = "Info.txt";

if (!Directory.Exists(System.IO.Path.Combine(path, dir)))
{
Directory.CreateDirectory(Path.Combine(path, dir));
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
else if (!File.Exists(Path.Combine(path, dir, fileName)))
{
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
else
{
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
}

Set text:


private string setText()
{
return "Done \r\n"
+ this.string1 + "\r\n"
+ this.string2;
}

setText can be whatever you want to write to the text file.


Split word into 2 variables (for example username:password):



string[] words = new string[2];
words = currentLine.Split(':');
string username = words[0];
string password = words[1];

There's probably a better way to do that :P

Zachafer
08-15-2015, 12:23 AM
For the last function, you only want to split the string once. Your example wouldn't parse a password that contained a colon :

j03
08-15-2015, 10:47 AM
Here is how I do a split in C#.NET:



string splitMe = "username:password";
string[] splitData = splitMe.ToString().Split(":".ToArray());

//username is stored in splitData[0]
//password is stored in splitData[1]

Coolguy Zachafer

Zachafer
08-15-2015, 12:38 PM
Here is how I do a split in C#.NET:



string splitMe = "username:password";
string[] splitData = splitMe.ToString().Split(":".ToArray());

//username is stored in splitData[0]
//password is stored in splitData[1]

[Only registered and activated users can see links]


string splitMe = "username:pas:sword";
string[] splitData = splitMe.ToString().Split(":".ToArray());

//username is stored in splitData[0]
//password is stored in splitData[1] AND splitData[2]

j03
08-15-2015, 01:03 PM
Ok but what if the password contains a colon? (A perfectly valid password character)


string splitMe = "username:pas:sword";
string[] splitData = splitMe.ToString().Split(":".ToArray());

//username is stored in splitData[0]
//password is stored in splitData[1] AND splitData[2]

Probably need a new separator OR write a function that specifically targets splitting the string where you utilize the length of the username while getting the password (for strings with birthdays added), otherwise you can just split the string once. :P

Zachafer
08-15-2015, 01:59 PM
string splitMe = "username:pas:sword";
string[] splitData = splitMe.Split(new char[] { ':' }, 2);

//username is stored in splitData[0]
//password is stored in splitData[1]