How to access a Textfile at a Server in ASP.Net MVC - c#

My ASP.Net MVC application is on a webserver and I have a Action in my Controller which reads the Textfile. The file is located in the wwwroot/Media folder. The problem is when I run the application, it seems that the programm cant find my File.
Here is the code with the access string:
string file = #"~/wwwroot/Media/TEST.txt";
using StreamReader csvReader = new(file);
I tried to run it locally it worked fine.
I tried with Server.MapPath(), it did not work

Related

How to read txt file on Client from aspx c# website

i have a question, I have this code
foreach (string line in File.ReadLines(**#"C:\fis32v6\fis32.ini"**))
{
if (line.Contains("TEST1"))
{
Label1.Text="TEST1";
PdLine = "1";
}
}
DataSet ds;
ds = GetData(PdLine.ToString());
I want to read from txt file on client specific line with condition. When developing this and building the code it works, what ever i change in txt file can be read from my PC. But when I run the website on server it reads the txt file on that server instead of client I opened the website.
Is there any possibility to make the path relative?
As John mentioned that would be a huge security issue, mostly to make sure the website doesn't dig around in your system.
However it can be instigated from client side.
Just have search here on SO for 'upload file using asp.net' there are loads of hits with answers listed.
You didn't mention specific versions you use (MVC?, asp.net? / core?), and no context as to what workflow your code is running in (is it run on connection or during a specific process), is it configuration settings used for the web session itself? but it is possible to upload.
Should you require the file during startup that might be a bit trickier as you'd have to upload it somehow.
If however it is settings for the web session, why not save it in a cookie?

Write text to the text file on server

I'm trying to write text to file on server from winform desktop application
string path = "http://www.site.info/doc.txt";
To use path:
System.Web.HttpContext.Current.Server.MapPath(path);
also I tried this way:
System.Web.Hosting.HostingEnvironment.MapPath(path);
to write text into the text document:
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/doc.txt"), true))
{
_testData.WriteLine("TEXT");
}
Seems like I'm doing something wrong,
name Server "does not exists in current context".
Not sure how to use Server.MapPath.
it is in References as System.Web not System.Web.dll, not sure, but it must be same, and in using as System.Web;
Also I am using System.Net; so maybe I can do it with WebClient.
You are trying to modify the file on server which the server wont allow as this could be misused and harm the server. You can update the file through the website hosting this text file.
The Server.Map path should be used in the website where you want to modify the file. If the file is asp.net web form website then you can make a aspx page that will modify file for you. If it is MVC then you will need a Action method in Controller to modify file for you.
If you want you own modified copy then you can download it and save it locally the Winform application as suggested by Sadiq. You can also upload the file by again your server side must allow this.
Why are you using Server.MapPath in a winform desktop application.
Download the file using something like this:
WebClient webClient = new WebClient();
var filearray = webClient.DownloadData(path);
and then write it to your local after modification (if needed) using
File.WriteAllBytes(savefilePath, filearray);
And then upload using webClient.UploadData(address, filearray).

Set path to GeoIpDB in asp.net application

I'm having trouble setting path to GeoIp database in my asp.net application.
I have db file in my application root folder, but when i call
using (var reader = new DatabaseReader("GeoLite2-City.mmdb"))
exception "file not found" is thrown.
When i pass full local path #"d:\documents\vsprojects\app\GeoLite2-City.mmdb" to DatabaseReader, application is working. Is there a way to fix this, because this would not work when i upload app to host?
Edit: When i try to access file directly, with http://localhost:45500/GeoLite2-City.mmdb i get an 404.3 error.

asp.net MVC3 create folder where application can write in

I need to create a folder to use for storing files within it, in a .Net MVC3 application, but I think the problem is common to all ASP.Net platform.
Problem is I can create the folder, but cannot write the files, because System.UnauthorizedAccessException occurred.
I also tryed givin extra permission to the user currently running the web app, but nothing changes.
This is my code so far:
if (!System.IO.Directory.Exists(fullPath))
{
System.IO.Directory.CreateDirectory(fullPath);
var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var userName = user.Translate(typeof(System.Security.Principal.NTAccount));
var dirInfo = new System.IO.DirectoryInfo(fullPath);
var sec = dirInfo.GetAccessControl();
sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(userName,
System.Security.AccessControl.FileSystemRights.Modify,
System.Security.AccessControl.AccessControlType.Allow)
);
dirInfo.SetAccessControl(sec);
System.IO.Directory.CreateDirectory(fullPath);
}
string fullPathFileName = System.IO.Path.Combine(fullPath, fileName);
System.IO.File.WriteAllBytes(fullPath, viaggio.Depliant.RawFile);
Too bad, last line of code always throw System.UnauthorizedAccessException.
I'm not impersonating user in my app, everything run under a predefined user.
What should I do to create a folder and assure that the application can also create files within it?
Edited:
I also tryed to save the files in the App_Data special folder, but I still got the System.UnauthorizedAccessException error. Somebody can tell me why is that happening?
I hate to answer my own question when the problem is that stupid...
I'm just trying to save a file without a proper filename: you can see I'm using the fullPath variable both for creating the folder and for saving the file, instead of using the correctly created fullPathFileName.
Blame on me!
Use App_Data folder, quote from http://msdn.microsoft.com/en-us/library/06t2w7da%28v=vs.80%29.aspx :
To improve the security of the data used by your ASP.NET application, a new subfolder named App_Data has been added for ASP.NET applications. Files stored in the App_Data folder are not returned in response to direct HTTP requests, which makes the App_Data folder the recommended location for data stored with your application, including .mdf (SQL Server Express Edition), .mdb (Microsoft Access), or XML files. Note that when using the App_Data folder to store your application data, the identity of your application has read and write permissions to the App_Data folder.

ASP .NET C# get all text from a file in the web path

I'm trying to open a file that includes some data in a web directory that my C# is running. Basically just turns it into a string. I tried doing the following...
string email = File.ReadAllText("/orderforms/email_templates/file_to_include.txt");
I'm not sure if that is the correct method, but it seems there is a pathing problem, the whole path will change depending what web server it is running on.
This is the directory setup...
/Classes/Page.ascx.cs (the page that tries to read the text from the
file)
/orderforms/<one of multiple pages execute the above class here or in a sub directory
/orderforms/email_templates/file_to_include.txt
/orderforms/email_templates/file_to_include2.txt
What path and function should I use to read all of the contents of the file to a string?
Thanks
Try this:
string email = File.ReadAllText(Server.MapPath("~/orderforms/email_templates/file_to_include.txt"))
You need to use Server.MapPath:
http://msdn.microsoft.com/en-us/library/ie/ms524632(v=vs.90).aspx

Categories