Loading json file outside of wwwroot - c#

I store my language resource files as JSON and need to load and deserialize them using Newtonsoft.Json package.
It wouldn't make much sense to put these files in the wwwroot folder since they are not to be used or opened directly by the client.
I tried to load them using File.LoadAllText() but it tries to find those files in the wwwroot and won't look outside of that.
Is there anyway I can load them?

You can access any files your environment allows even though the working directory by default is set to wwwroot. It makes sense to store data that is not accessed directly by the client to other directories than wwwroot.
You can use the services at PlatformServices.Default.Application to get the path to the approot folder via ApplicationBasePath. This way you can be sure you are accessing the correct files without relying on working directory and relative paths.
To use this you have to add a reference to Microsoft.Extensions.PlatformAbstractions.

Related

How to access a subfolder within a asp.net (C#) Project, WebApi

I have a seemingly basic question, but I can't access a folder inside my project (actually, it is a project that is one of the references of the main project), which is a folder called "pdfs" that contains 1 PDF that I want to convert to base64.
I put the "pdfs" folder in the same level of the file from where I call it, as I thought it would be easier to access (actionDB.cs file, which is in services/actions/actionDB.cs). The "pdfs" folder is in services/actions/pdfs).
But there is no way to get the correct path of this folder.
On the contrary, if I put the folder in c: , getting path = "c:\\pdfs", and the pdf file there, I can easily find it and convert the pdf perfectly in the code.
Any idea how to access these folders that are inside the projects, as if they were assets? I already put 'copy always: to the destination' and 'type of compilation: content' in the pdf properties, but I still can't access it.
I have already tried the following options:
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"pdfs");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"services\actions\pdfs");
string path = Path.Combine(Path.GetDirectoryName(Environment.CurrentDirectory), #"services\actions\pdfs");
The namespace of the actionsDB is ProjectName.Data.services.actions.
I got results such as IIS/.., or other paths inside VS installation…, Assembly and some weird hashes.
Nothing works...
Could you help me with this?
Thank you very much,
CBD
AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.

How to get absolute path of another project in a solution C#

I have a solution containing two projects. One project is just for doing all data stuff and the other one, the startup project, do all the web stuff.
Now I want to get the TasksDataBase.xml from the TaskManagerHelpers class by first getting the projects root directory. But all I get is the TaskManager.Web root directory. (I call the method inside TaskManagerHelpers.cs from a controller inside TaskManager.Web)
How do I get the TaskManager.Data root directory when I'm in a class in the same project?
I've tried with theese methodes and similar ones.
HttpContext.Current.Request.PhysicalApplicationPath;
System.IO.Path.GetFullPath();
AppDomain.CurrentDomain.BaseDirectory;
Thanks in advance!
One possibility is to embed the XML file into the assembly of the class library and then read it as resource in your web application. Remember that when you publish your web application to a web server all that will get into the package will be the files of this web application. There's no physical relation to some projects that might have lived into the Visual Studio solution that this web application was part of.
You may take a look at the GetManifestResourceStream method which will allow you to read the embedded XML from the referenced assembly.
Here's an example:
// you could use any type from the assembly here
var assembly = typeof(TaskManagerHelper).Assembly;
using (var stream = assembly.GetManifestResourceStream("TaskManager.Data.DataBase.TasksDataBase.xml"))
using (var xmlReader = XmlReader.Create(stream))
{
// ... do something with the XML here
}
Bear in mind though that since the file is embedded into the assembly you will not be able to modify it. It is readonly. If you need to modify it then an alternative approach would consist into copying this file to your web application. For example a good place is the App_Data special folder. You could even setup a post compilation step that will copy the XML file in this location.
And then you can reference it easily:
string xmlFile = HostingEnvironment.MapPath("~/App_Data/TasksDataBase.xml");
using (var xmlReader = XmlReader.Create(xmlFile))
{
// ... do something with the XML here
}
In this case since the XML file is now physically part of the web application and lives on the hard drive you could also modify it.
Just because the two projects are located in the same folder tree during development, says nothing about where they'll be located at run time. It's entirely possible that that could be on different machines.
"No," you say. They'll will definitely be on the same machine in the same c:\inetpub tree. That may be true, but that's your policy, not a requirement.
If you are going to establish a hard policy about where they are located, then you can hard-code that into you code.
Right-click the XML file and select properties, then change the Copy to Output Director to one of the other settings than "Do Not Copy". That will place the file into your \bin\ folder alongside the other project output. You can then use AppDomain.CurrentDomain.BaseDirectory as your base path
IF you are running a web project, all the referenced dll files are copied to the bin directory (unless they are in the GAC) and used from there, no matter if you add a reference to another project, Visual Studio first compile it and then copies it to the bin folder of the web project. You can mark your xml file as Content (Compilation Action) and with the copy always option so it always copy it to the bin directory .... the problem is that it sometime look for this files outside of the bin folder but I think that you can handle this.

C# Xml files when creating exe application

I'm planning to build my winform into a .exe file. I'm just wondering what to do with the XML files that my application needs?
I did some research and found out that I can add the XML files in the Resource folder before creating a .exe file.
Or I need to create a setup file? When the user runs the setup file, the XML files will be installed into their pc.Now I wonder which one is the best way to go for,
Note: XML files might get modified by the user.
If you want to ship the XML files as seperate to the .EXE then you can set the Copy to Output Directory to Copy if newer. (click on file and then go to properties).
OR if you want it as part of the .EXE I think you can change the Build Action to Embedded Resource.
I personally would create a Setup as per your edit and include the XML files. I usually just add everthing from the bin/release folder that is needed when I create a setup file.
You could either deploy the necessary files along with the executable in the same folder or embed them as resources (if they are read-only). If you need to modify them do not embed them as resources into the executable.
The correct way depends on how you intend to use the files. If the files always are deployed together with your application, the application never writes to them and they are never upgraded without upgrading the application, you can go with them embedded as resources.
If you need to update them separately from the application, you need to have them as physical files.
You don't necessarely need a installation package, unless you need to apply some logic during setup, such as updating the content of the setup based on user input or to check preconditions. The application can just be copied into place regardless of if you have embedded the files or not.

ASPX: Get path of cs file

How can I get the path of a cs file in aspx?
Here's the issue: I've created a property class in a property.cs file which is used in an EPiServer admin module. Server.MapPath() will hence return the path of the executing file, which is in a totally different place than my code. I need to get the path of the property.cs file (from inside the property.cs file) in order to dynamically set some relative paths to css and js files. How can this be done?
I hence want to be able to include .js and .css files in this cs file, all files located in the same directory, but the cs file is accessed from the EPiServer UI.
I would highly recommend not doing what you are trying to do. You'll be constructing a brittle dependency on files that should not even by deployed with your project.
If you have web classes that rely on resources like javascript and css, you should use the ClientScriptManager (or ScriptManager for ajax apps) to register the script files onto the page, and the scripts and css should be deployed into their own regular web directory.
If deployment location is a problem, and you're creating some kind of reusable, redistributable module, then I would recommend that you embed the .js and .css files as WebResources in your assembly, and use the script manager to register the scripts to the page that way, with ClientScriptManager.RegisterScriptResource().
I think you should go with including your CSS, JS and other static files as embedded resources.
That will include the files inside the DLL, which makes deployment easier. You can then set up a HTTP handler which will serve you the contents of the embedded files - or use the aforementioned RegisterScriptResource() method.
By embedded the files you don't have to know any file paths.
ASP.NET is compiled, you should never need to read settings from source files at runtime, you should read from configuration files (*.config), if they need to be dynamic, these too can be injected during the page lifecycle via a variety of methods.

Relative paths in C#

I need some help with paths please!
Basically I have a project with the following folder structure:
Project (root directory which contains the .sln file etc.)
Project/MyProj (contains the code)
Project/MyProjTest (the test folder)
Project/TestResults
Now with this Project I need to have a common folder where I can stick a bunch of files for use with the Application without having to copy the files to multiple locations etc. What is the best way to do this? Ideally I would like to have the folder as Project/ResourcesFolder, so that both the Code folder and Test folder can access it. Now if this is the case how do I call this folder from within C#? I've tried Application.StartupPath, Environment.GetCurrentDirectory but they both just return the CURRENT folder which is not what I want.
Thanks in advance.
You can add a solution folder to your solution and place common files in it.
You'll have to copy the files, you'll want your program to operate the same way after it is deployed. The simplest way to do so is by adding them to your project. In the Properties window, set Build Action = None, Copy to Output Directory = Copy if Newer. The latter setting ensures that you don't waste time copying the files over and over again.
This ensures that the files will be present in the same directory as your EXE. Both when you debug and after you deploy it. Simply use Application.StartupPath to locate them. Creating the Setup project for the app is now very simple as well.
Note that if the files are small you really want to embed them as resources.
.. goes one directory up. That is, from Project/MyProjTest you could access Project/MyProj via ../MyProj.
Use Server.MapPath("~") to get to the root folder of your application. From there you can get to wherever you need.

Categories