Some useful c# snippets from lokiys

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 811
Благодарностей
1 187
Баллы
113
Very useful snippet when you want to use action keyboard emulation and want to press, example DOWN button random times.
C#:
// Generate random nr.
Random r = new Random( );
int countFrom = 5; // Change number to your needed from count
int countTill = 10; // Change number to your needed till count
// Random nr
int randomNr = r.Next(countFrom, countTill);
// What to repeat
string word = "{DOWN}"; // You can change word "{Down}" to whatever you need
// Building string
string repeatWords = String.Concat(Enumerable.Repeat(word, randomNr));
return repeatWords;
Another useful snippet for deleting blank lines from .txt file, saw many questions in forum about that
C#:
// Delete blank lines from file
string path = project.Variables["filePath"].Value; // Gets file path from your project variable with name "filePath"
var lines = System.IO.File.ReadAllLines(path).Where(arg => !string.IsNullOrWhiteSpace(arg));
System.IO.File.WriteAllLines(path, lines);
return 0;
When you working with files copy them or move then you should determine new file location and for that is file extension needed. Quick way to get that.
C#:
// Get file extension
string path = project.Variables["filePath"].Value; // Gets file path from your project variable with name "filePath"
string ext = Path.GetExtension(path);
return ext;
Get root domain quickly
C#:
// Get root domain
var url = project.Variables["url"].Value;
return new Uri(url).Host;
Count words in text
C#:
// Count words in string
var inputstring = project.Variables["text"].Value;
string texttostring = (inputstring);
int count = texttostring.Split(' ').Length;
return count;
Count characters
C#:
// Count characters
string stringToCount = "Hello World";
return stringToCount.Length.ToString();

Cheers
 

Alouka

Пользователь
Регистрация
30.07.2015
Сообщения
89
Благодарностей
5
Баллы
8
Thank you lokiys ! Nice C# snippets :-)
 
  • Спасибо
Реакции: lokiys
Регистрация
10.07.2017
Сообщения
29
Благодарностей
1
Баллы
3

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)