Hey guys i need to solve this problem and I am completely lost.
- 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
-
1Try 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– PapaAtHome2022年02月03日 16:24:24 +00:00Commented Feb 3, 2022 at 16:24
2 Answers 2
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.
-
its giving me this "Server Error in '/' Application". and under that it write the content of the fileDan– Dan2022年01月26日 09:48:53 +00:00Commented 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?Rahul Sharma– Rahul Sharma2022年01月26日 09:50:26 +00:00Commented Jan 26, 2022 at 9:50
-
-
Ok, then it should read the file:
addresses.txt
. What error are you getting?Rahul Sharma– Rahul Sharma2022年01月26日 09:56:39 +00:00Commented Jan 26, 2022 at 9:56 -
Server Error in '/' ApplicationDan– Dan2022年01月26日 10:13:51 +00:00Commented Jan 26, 2022 at 10:13
Use this
var fileContents =
System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/App_Data/file.txt"));