How to make my program write result to a file?

  • Thread starter Thread starter BataBo Jokviu
  • Start date Start date
B

BataBo Jokviu

Guest
So I want my program to write results to a file.I've already tried with StreamWriter but and it worked but than I implemented multi threading and now it constantly returns error 'The process cannot access the file[REDACTED] because it is beign used by another process'

Here is my code and what I triad:

class Program
{
static Regex idcheck = new Regex(@"id"":""(.+?)"",");
static Regex Migrationcheck = new Regex(@"legacy"":(.+?)}");

private static String _outputFileUnmigrated = "Unmigrated.txt";
private static String _outputFileMigrated = "Migrated.txt";

static void Main(string[] args)
{
Console.Write("Threads : ");
int threads = int.Parse(Console.ReadLine());



var lines = File.ReadLines("List.txt");
var UnmigratedNumber = 0;
var MigratedNumber = 0;
Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;


Parallel.ForEach(lines, new ParallelOptions { MaxDegreeOfParallelism = threads }, name =>
{
using (StreamWriter UnmigratedWriter = File.AppendText(_outputFileUnmigrated))
{
using (StreamWriter MigratedWriter = File.AppendText(_outputFileMigrated))
{
// foreach (var line in lines)
{
args = new[]
{
"https://api.mojang.com/users/profiles/minecraft/" + name
};

using (var client1 = new WebClient())
{

var content = string.Empty;
var porxies2 = File.ReadLines("Proxies.txt");
foreach (var proxy in porxies2)
{
WebProxy wp = new WebProxy(proxy.ToString());
client1.Proxy = wp;
try
{
content = ScrubContent(client1.DownloadString(args[0]));
break;
}
catch (Exception ex)
{
Console.WriteLine("BAD PROXY {0}", proxy);
}
}

var idchecked = idcheck.Matches(content).Cast<Match>().Single().Groups[1];

args = new[]
{
"https://sessionserver.mojang.com/session/minecraft/profile/" + idchecked

};
using (var client2 = new WebClient())
{
var content1 = string.Empty;
var porxies = File.ReadLines("Proxies.txt");
foreach (var proxy in porxies)
{
WebProxy wp = new WebProxy(proxy.ToString());
client2.Proxy = wp;
try
{
content1 = ScrubContent(client2.DownloadString(args[0]));
break;
}
catch (Exception ex)
{
Console.WriteLine("BAD PROXY {0}", proxy);
}
}

try
{
var migrationchecked = Migrationcheck.Matches(content1).Cast<Match>().Single().Groups[1];
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("UNMIGRATED " + name);
UnmigratedWriter.WriteLine(name);
UnmigratedNumber++;
Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;

}
catch (InvalidOperationException ex)
{

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("MIGRATED " + name);
MigratedWriter.WriteLine(name);
MigratedNumber++;
Console.Title = "Cookie Legacy checker | by BataBo | Checked: " + (UnmigratedNumber + MigratedNumber) + " | Good:" + UnmigratedNumber;
}

}
}
}




}
}
});
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("====================Results====================");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Checked: {0} names", UnmigratedNumber + MigratedNumber);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Unmigrated: {0} accounts", UnmigratedNumber);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Migrated: {0} accounts", MigratedNumber);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("====================Results====================");
Console.ReadLine();
}

static string ScrubContent(string content)
{
return new string(content.Where(c => c != '\n').ToArray());
}
}
}

Continue reading...
 
Back
Top