Path to content folder in asp.net mvc - c#

I'm trying to read a .txt file in my program:
using (StreamReader sr = new StreamReader(VirtualPathUtility.ToAbsolute("~/Content/txt/FamilyNames.txt")))
{
String line = sr.ReadToEnd();
Debug.WriteLine(line);
}
This however gives me following path, which is incorrect:
C:\Content\txt\FamilyNames.txt
When I search for this I come up with numerous solutions like:
Server.MapPath();
But this seems to be outdated code? Because it doesn't get recognized in my Visual Studio, can't import it...
So what is the correct solution to get a path to a file in the content folder?

Server.MapPath needs a HTTPContext. Use System.Web.Hosting.HostingEnvironment.MapPath instead.
using (StreamReader sr = new StreamReader(HostingEnvironment.MapPath("~/Content/txt/FamilyNames.txt")))

Have you tried :
StreamReader(VirtualPathUtility.ToAppRelative("~/Content/txt/FamilyNames.txt")))

Related

How to find path of file in MAUI for all platforms?

I'm making an app that I want to export to both Windows and Android. In this piece of code, I'm trying to read from a .csv file:
using (StreamReader csv = new StreamReader("MOCK_EMPLOYEE_DATA.csv"))
However, It keeps giving me an error that the file was not found and the path was incorrect. What is the best way to go about this?
The file is in my project folder.
You could do that by following these steps:
1.Drag or add the csv file to the Project.
2.Set the Build Action of csv file to MauiAsset.
3.use the following code to read the file:
using var stream = await FileSystem.OpenAppPackageFileAsync("File.csv");
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
Console.WriteLine(contents);
Hope it works for you.

Getting the contents of a file in Visual Studio without opening the file from an extension

I'm trying to read the contents of a file in a Visual Studio extension. The following code works, but forces me to open the file, if it isn't (otherwise it crashes):
textDocument = (TextDocument)projectItem.Document.Object("TextDocument");
EditPoint editPoint = textDocument.StartPoint.CreateEditPoint();
string text = editPoint.GetText(textDocument.EndPoint);
I can get the path of the project, so I suppose I could make an educated guess as to the location of the project item. However, ideally I'd like to either get the file contents without opening it; or, alternatively, get the path to the project item (then I could just use System.IO to access the file contents).
I've looked, but don't seem to be able to find any mention of either of these. Can anyone point me in the right direction, please?
You can get the path from a ProjectItem by reading its properties.
var path = YourProjectItem.Properties.Item("FullPath").Value.ToString()
After you have the path you can read its content with System.IO.
string content = File.ReadAllText(path);
If the file is somewhat larger and you are getting troubles with the current code due to size, you should take a look at the StreamReader class.
I'm not sure if this is possible for extensions but you could probably use System.IO, like this:
using System.IO;
string filePath = #"C:\Whatever\YourFileName.txt";
string fileText = File.ReadAllText(filePath);
You could also use StreamReader like this:
using System.IO;
string filePath = #"C:\Whatever\YourFileName.txt";
using (StreamReader sr = new StreamReader(filePath))
fileText = sr.ReadToEnd();
EDIT:
I think I understand you better now.
The only way to "get the file contents without opening it" would be if the extension were to give you that data actively, but I can safely assume it doesn't.
When reading a file, you should already know where the file is (if you don't know then either you're not intended to access that file or you just haven't looked long enough).
I'd try searching the SDK files manually (Or with a file crawler).

Android text file not found

I have a text file in my solution called "txtWords.txt", which I attempt to read with:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, "txtWords.txt");
In my experimenting, I've got the same file under both my C# App and the App.Android > Assets folder (set to AndroidAsset). I thought to put it in both locations to be safe.
System.IO.FileNotFoundException: Could not find file "/data/user/0/com.WSC/files/txtWords.txt"
I've read about using AssetManager but that gives me a "namespace could not be found error".
What am I doing wrong and what's the best way to read a text file for an app? This should be really easy so I'm doing something fairly basic wrong, I suppose.
Using Xamarin Essentials you can read a bundled/asset read-only file via your NetStd (Xamarin.Forms) library:
string wordsText;
using (var stream = await FileSystem.OpenAppPackageFileAsync("txtWords.txt"))
using (var reader = new StreamReader(stream))
{
var wordsText = await reader.ReadToEndAsync();
}
re: https://learn.microsoft.com/en-us/xamarin/essentials/file-system-helpers?tabs=android
Nuget: https://www.nuget.org/packages/Xamarin.Essentials

asp.net web form C# Streamreader can't find the text file

Trying to make a silly web form that keeps some small information in a text file inside a "data" folder. The code is:
StreamReader sr = new StreamReader(Server.MapPath("data/FeatureList.txt"));
String FileText = sr.ReadToEnd().ToString();
sr.Close();
Getting an exception:
Could not find file 'C:\Users\Tom\Documents\Visual Studio 2013\WebSites\WebSite2\data\FeatureList.txt'.
And, naturally, the file is there right in the specified folder.
Perhaps I have a permission error and IIS can't read the folder or file? Maybe I need to tell Visual Studio 2013 something about this folder? I haven't played with one of these asp.net programs in a while, not since... Well, a couple years ago I made a web page that reads text files and adds captions to displayed photos.
You may use the Path.Combine , it is a good method to get a valid path
StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~"), #"data/FeatureList.txt"));
String FileText = sr.ReadToEnd().ToString();
sr.Close();
Try,
string path = (string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory,"\data\FeatureList.txt));

How can I open a text file relative to my MVC project?

In my bin file I have set up som test data, and I want my application to be able to access logfiles that are stored in bin/log/log00001.txt.
However, in my crontroller, when I try to use a TextReader on the following path it goes somewhere else: new StreamReader("log/log00001.txt")
How do I read stuff relative to my project?
Try using
StreamReader reader = new StreamReader(Server.MapPath("~/bin/log/log00001.txt"));
HttpContext.Current.Server.MapPath("~/some/path/relative/to/your/web/app")

Categories

Resources