调试和正常执行之间的不同结果C#

时间:2019-06-20 15:04:26

标签: c# debugging filesystemwatcher system.io.file

我的程序读取一个文本文件并将其导入数据库。当我执行该文件时就会出现问题,文件每30秒出现一次,并且在第一个文件到达后,我收到以下错误消息:该进程无法访问该文件,因为该文件已被其他进程使用。当我调试时,我没有收到错误,所以我看不到是什么导致了此错误。在调试模式下一切正常...

using (MySqlConnection connection = new MySqlConnection(Helper.CnnVal("TechDatabase")))
{
    connection.Open();

    try
    {

        List<string> properties = File.ReadAllLines(filePath).ToList();
        ManureManagementProp newManureProp = new ManureManagementProp
        {
            Water_level = properties.ElementAt(0),
            Wind_speed = properties.ElementAt(1),
            Food_level = properties.ElementAt(2),
            Animal_count = properties.ElementAt(3),
            Number_of_machines = properties.ElementAt(4),
            Machine_id = properties.ElementAt(5),
            Date_time = DateTime.Now
        };


        string query = "INSERT INTO..."; 

        /*Put values in db*/

        MySqlCommand command = new MySqlCommand(query, connection);
        MySqlDataReader reader = command.ExecuteReader();

        reader.Close();

        Console.WriteLine("Updated");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    connection.Dispose();
}

1 个答案:

答案 0 :(得分:0)

问题来自FileSystemWatcher。我遵循了this,它可以正常工作!