no acces to .txt with Webmatrix ASP.NET - asp.net

I got a tiny website for my company to check in which store a brand can be found.
The website uses a c# code to check .txt files in a subfolder.
It worked nice while I used a path like "C:\brands" on my pc but now I try to host the homepage so I changed the path to "~/App_Data/Brands/Normal" + StoreNames()[i] + ".txt".
But now I can't access the .txt files anymore.
The txt files are located in root/App_Data/Brands/Normal but the code is in root/App_Code. Could that be a problem?
Also I'm trying to access the files without any special permissions or an account.
The host is https://panel.sitecloud.cytanium.com/.
What do I need to get access to the files again?
It could be a problem with my code too because it doesn't work on localhost either (where it worked with an extern folder)..
Edit:
Okay, MapPath did the job! I just forgot to add MapPath also to
if(File.Exists(HttpContext.Current.Server.MapPath(sFilename)))..

Try to use HttpContext.Current.Server.MapPath to get the physical directory:
string data = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/TextFile.txt"));

Related

ASP.Net web application cannot read a file within folder

In my asp.net web application, I read the xml file for obtaining a key. If file is not present I show a form to enter the key details and then create the file.
First problem: My app does not recognize the file even if its there.
Second problem: I am running application on the server. When writing, rather overwriting the file, browser shows the username, password prompt before writing the file. If I enter admin credentials it allows to create a file.
I have checked all possible combinations of permissions on the file / folders, but could not resolve the problem.
Any ideas, what I could be missing here?
You read the xml file but is it as a part of your solution? If yes, are you reading it through relative path i.e. are you using Server.MapPath to read it like Server.MapPath("~/Files.test.xml")? Once you use relative path, I don't think it will ask you credentials as it still is in your project directory.
It should work. I am also reading and writing files in my web application.
If it still does not work, please tell me the way you are reading file.
Thanks,

Specific path to a text file on an ASP.NET server application

I would like to set a path to my text files which are stored in TextFiles folder. Project looks like that:
This application is already hosted on a website. I've tried almost every combinations of Server.MapPath like ("~/TextFiles/UserItemReturnMail.txt"), ("./TextFiles/UserItemReturnMail.txt"), ("\\TextFiles\\UserItemReturnMail.txt"), etc. How can I get to those files, because I have no idea now.
Try this :
Server.MapPath("."), Server.MapPath("~"), Server.MapPath(#"\"), Server.MapPath("/"). What is the difference?
and
How to use Server.MapPath to get location outside website folder in ASP.NET
and this official :
https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx

Server.MapPath does not find the path on Azure

I have deployed my project to Azure. In my project I have "App_Data\Images" folder.
Now I'm trying to do the following:
String filename = GLOBAL_IMAGES_VALS.GET_FILE_PREFIX(imageType) + "-" + User.Identity.GetUserId<int>().ToString() + Path.GetExtension(image.FileName);
String origPath = Server.MapPath("~\\App_Data")+"\\Images\\" + filename;
But then upon trying:
image.SaveAs(origPath);
I get this error message:
Could not find a part of the path
'D:\home\site\wwwroot\App_Data\Images\logo-10003065.jpg'.
How can I save my file to "App_Data\Images\"?
The actual problem was that the sub-folder 'Images' did not exist. I can't remember why the publish process did not create this sub-folder, however I added it manually and then everything worked fine.
EDIT:
As others wrote here (#Spectarion). I'll put here the important remark that explain why the folder was not created:
Just for the future readers, folder won't be created if it's empty.
Folder is empty even if there are files that are not included in
project.
Just put some 'fake.txt' file into any folder you want to make sure that it will be created, and of course don't forget to add it to the project. Good luck.
Since you don't have any file in the particular folder, while publishing Web deploy ignores the empty folder.
Quick fix: Add any file to the folder before publishing will fix this issue.
if (!Directory.Exists(Server.MapPath("~/Images")))
{
Directory.CreateDirectory(Server.MapPath("~/Images"));
}
The directory might be missing in the folder. Create the directory and use it in file path
Maybe this :
System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data")+"\\Images\\" + filename )
Maybe the images folder doesn't exist and you need to create it first? Although I wouldn't recommend saving images in your app like this if it is designed for people uploading images. I would save them in Azure storage via blobs or the new Azure File storage. I would keep your app deployment files clean just related to your app and save any user generated content outside of it.
BTW, If you are using Azure Web Apps you can use the environment variable of "HOME" to always get the correct path (which should be D:\home)
string path = Environment.GetEnvironmentVariable("HOME") +
"\\site\\wwwroot\\App_Data\\images"
I assume your AppData folder is just under the wwwroot folder, which is usually the case.
Try this:
HttpContext.Current.Server.MapPath(Path.Combine("~/AppData/Images/", filename));
I just had this problem on VS15. I first followed the advice in this question in order to generate the error you've got. I'm guessing this follows in part dsb's answer, but dsb hasnt given any description of the actual process of fixing this.
I then went to https://<mywebsite>.scm.azurewebsites.net/DebugConsole to look through the directory and found that App_Data had not been published
Which was why the error was throwing. So, I then solved this by simply going to the solution explorer, right clicking App_Dataand selecting to "Publish App_Data".
However, my website was a short-term academic effort for a project - I think there is probably a lot to be said for considering Matt Watsons answer above about whether or not allowing users to upload to the deployment area is a good idea

Drupal: change path to uploaded files

I copied a site files to test host for further developement.
And I want uploaded files to be loaded from old host.
For example, files a re stored in drupal_folder/sites/default/files/ (example.com/sites/default/files).
I need this files to be accessible from other site (example.dev.com)
I mean I want to change path to all files on a new host (example.dev.com).
I tried to change path to all files in database with command
UPDATE `files` SET `filepath` = REPLACE(`filepath`, "sites/default/files/", "http://example.dev.com/sites/default/files/");
but URLs are like example.dev.com/http://example.dev.com/sites/default/files/ after this.
I hope it makes sence.
How can I do it?
Use ln to create the link to that directory if you are using linux.
http://linux.about.com/od/commands/l/blcmdl1_ln.htm

Downloading files

I am using asp.net/C#. I have a url to a folder on a remote server. In that folder are images that are all .jpgs. I don't know the names of the files, just that they are all .jpgs. How do I download all the .jpg images using asp.net/C# to a folder on my local hard drive.
Just to clarify. I am pulling files from a remote server and I am saving them to my local machine. I was given a web URL and told that the files I needed to pull down every night where .jpg image files. That's all I was told. I have no idea how I can get a list of files on a remote server with just the url to the folder.
Thanks
If it's a web URL, you'd have to depend on the web server giving you some sort of list of files. The format of the list could be almost anything.
Put it this way: using a browser or anything else, how would you as a human find out all the filenames?
Just to clarify, are you writing code on the server which has the files? If so, you can find out what files are present using Directory.GetFiles. What do you want the user to have to do at the local side?
If you could make your question a bit clearer it would really help.
Here is some concept code to work with
DirectoryInfo di = new DirectoryInfo("M:\MappedDrive");
FileInfo[] rgFiles = di.GetFiles("*.aspx");
foreach(FileInfo fi in rgFiles)
{
Response.Write("<br>" + fi.Name + "");
}

Resources