There is a file in my application. I want to use this file to write data(which is coming from a machine connected on LAN) as well as for reading data from it and writing in a database. Therefore, I am placing a condition to check if the file is free.
This is my code:
static public bool IsFileFree(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return false;
}
finally
{
if (stream != null)
stream.Close();
}
return true;
}
static public void dataread()
{
FileInfo info = new FileInfo("C:\\Users\\cdac\\Desktop\\server\\server\\TextFile2.txt");
while (true)
{
if (IsFileFree(info) == true)
{
byte[] bytesFrom = new Byte[1000];
Program.socarray[0].Receive(bytesFrom);
char[] stuffed = System.Text.Encoding.UTF8.GetString(bytesFrom).ToCharArray();
char[] final;
final = stuffed.ToArray();
string foo = new string(final);
System.IO.File.WriteAllText(@"C:\Users\cdac\Desktop\server\server\TextFile2.txt", foo);
System.Threading.Thread.Sleep(10);
}
else
{
MessageBox.Show("File is already ", "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}
}
static public void datawrite()
{
FileInfo info = new FileInfo("C:\\Users\\cdac\\Desktop\\server\\server\\TextFile2.txt");
while (true)
{
if (IsFileFree(info) == true)
{
string MyConString = "server=localhost;" +
"database=cdac;" +
"User Id=root;" +
"password=cdac56;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
connection.Open();
// StreamReader reader = new StreamReader("C:\\tag_log_030610.txt");
StreamReader reader = new StreamReader("C:\\Users\\cdac\\Desktop\\server\\server\\TextFile2.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(';');
//command.CommandText = "insert into st_attn(rollno,Th_attn,Name) values('" + parts[0] + "','" + parts[1] + "','" + parts[2] + "')";
command.CommandText = "insert into st_attn(rollno,Name) values('" + parts[0] + "','" + parts[1] + "')";
Reader = command.ExecuteReader();
}
System.Threading.Thread.Sleep(10);
}
else
{
MessageBox.Show("File is already in use", "My Application",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
int i=0;
TcpListener listener = new TcpListener(8888);
listener.Start();
while(true)
{
Socket soc = listener.AcceptSocket();
socarray[i] = soc;
i++;
if (i == NUMBEROFREADERS)
break;
}
Thread writetotextfile = new Thread(dataread);
writetotextfile.Start();
Thread writetodatabase = new Thread(datawrite);
writetodatabase.Start();
Application.Run(new Form1());
}
When i execute it, my application is not writing anything in textfile and the data which is already existing in this text file is being written in the database with every execution or this application.
I want my application to write data in the text file which is not being written by it currently. Moreover both the processes( one that is for writing data in text file and other for writing data in DB from text file) when make conflict i want to give preference to one which is writing data in text file and want the other process to wait till then. And as soon as data is written in text file and it is free, the data is to be written in database from text file.