-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesControl.cs
96 lines (82 loc) · 2.57 KB
/
FilesControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
File IO - Write to .txt output file all the scrapped emails
1. Write lines to file (writeToFile)
*/
namespace whois_scrapper
{
public class FilesControl
{
//Variable to lock access to only one thread
private static Object fileLock = new Object();
private static String filePath;
public static String fileName = "\\whoisOutput.txt";
//Write line to output file
public static bool writeToFile(string line)
{
//Use actual executable path
filePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
bool notSaved = true;
while(notSaved)
{
try
{
lock (fileLock)
{
//Create of use actual file
if (!File.Exists(filePath))
{
StreamWriter sw = File.CreateText(filePath);
}
//Write lines on the file
using (StreamWriter sw = File.AppendText(filePath))
{
sw.WriteLine(line);
notSaved = false;
}
}
}
catch (IOException e)
{
notSaved = true;
}
}
return true;
}
public static bool writeText(String text)
{
filePath = AppDomain.CurrentDomain.BaseDirectory + "\\whoisOutput.txt";
bool notSaved = true;
while (notSaved)
{
try
{
lock (fileLock)
{
//Create of use actual file
if (!File.Exists(filePath))
{
StreamWriter sw = File.CreateText(filePath);
}
//Write lines on the file
using (StreamWriter sw = File.AppendText(filePath))
{
sw.Write(text);
notSaved = false;
}
}
}
catch (IOException e)
{
notSaved = true;
}
}
return true;
}
}
}