check if string exists in a file

后端 未结 7 1684
猫巷女王i
猫巷女王i 2021-02-09 19:36

I have the following piece of code which opens a text file and reads all the lines in the file and storing it into a string array.

Which then checks if

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 20:16

    Try try catch:

    string x;
    
    string log = @"C:\Users\Log.txt";
    
    string ruta = @"C:\Users\x.txt";
    
    if (File.Exists(ruta))
    {                    
        try
        {
            x = File.ReadAllText(ruta);  
        }
        catch (Exception ex)
        {
            File.AppendAllText(ruta, "Something");
            File.AppendAllText(log, Environment.NewLine + DateTime.Now.ToString() + ": The file not contain a string. " + ex.Message);
        }
    }
    

提交回复
热议问题