-2

Hey guys i need to solve this problem and I am completely lost.

  1. In root application create txt file, for example addresses.txt a in there will be dates in format: Name;Path;number_of_days;

2: In method Index load this .txt, where could be 10 rows with addresses. I need to load every row with string.Split separator will be semicolon(;) This will be load to List object. Then it will go through the foreach and you will go through the individual addresses via File.Getfiles(), where the input parameter is the directory. your files will be loaded, which you pass and if neither of them is younger than number_of_days, you note the error for this entry

Dan
  • 1
  • 1
    Try google for 'c# how to read/write text files' and you find many examples. Don't expect to get some code made for you when you post a description on what needs to be done. People here have a job to do and you need to do your home work first. – PapaAtHome Feb 03 '22 at 16:24

2 Answers2

0

You can use the following method to read a .txt file from the root folder:

    public string LoadTextFile()
    {
        string response = string.Empty;
        try
        {
            string myTxtFile = System.Web.Hosting.HostingEnvironment.MapPath("~/addresses.txt");
            if (File.Exists(myTxtFile))
            {
                using (StreamReader reader = new StreamReader(myTxtFile))
                {
                    string content = reader.ReadToEnd();
                    response = content;
                }
            }
            else
            {
                response = "File not found, error msg :" + myTxtFile;
            }
        }
        catch (Exception ex)
        {
            response= "Error in getting function LoadTextFile, Error msg :" + ex.Message;
        }

        return response;
    }

Your response string will contain the contents from the file that is read.

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
0

Use this

var fileContents = 
    System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/App_Data/file.txt"));
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129