通过两个进程访问文件

时间:2014-08-07 06:17:49

标签: c# file

我的应用程序中有一个文件。我想使用这个文件来写入数据(来自LAN上连接的机器),以及从中读取数据和写入数据库。因此,我提出了检查文件是否空闲的条件。

这是我的代码:

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());
}

当我执行它时,我的应用程序不会在textfile中写入任何内容,并且每个执行或此应用程序都会在数据库中写入此文本文件中已存在的数据。

我希望我的应用程序在当前没有写入的文本文件中写入数据。此外,两个进程(一个用于在文本文件中写入数据,另一个用于在文本文件中写入数据)在发生冲突时我想优先考虑在文本文件中写入数据并希望其他进程等到然后。一旦数据写入文本文件并且它是空闲的,数据就会从文本文件写入数据库。

0 个答案:

没有答案