-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

asked Jan 26, 2022 at 8:31
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. Commented Feb 3, 2022 at 16:24

2 Answers 2

1

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.

answered Jan 26, 2022 at 8:58
10
  • its giving me this "Server Error in '/' Application". and under that it write the content of the file Commented Jan 26, 2022 at 9:48
  • @Dan Put a break-point when you invoke the method to see if you are getting any errors. Also where are you placing your file? Commented Jan 26, 2022 at 9:50
  • in a root of application Commented Jan 26, 2022 at 9:53
  • Ok, then it should read the file: addresses.txt. What error are you getting? Commented Jan 26, 2022 at 9:56
  • Server Error in '/' Application Commented Jan 26, 2022 at 10:13
0

Use this

var fileContents = 
 System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/App_Data/file.txt"));
Super Kai - Kazuya Ito
41.9k20 gold badges253 silver badges249 bronze badges
answered Feb 10, 2022 at 13:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.