How to get static file directory - c#

In my solution, I have static file directory at project ApiCon and my code is need to get static file directory is in project BLL and the file path url is /image/myimg.png.
Now that file can show in front end completely. I need to delete my image in static file directory when I call Api in controller, But I can't get path to delete that file.
How to get static file directory?
if (File.Exists(path))
P.S. I need not to use Directory.GetParent() for find directory in other project.

Static files, while they should be located in the application folder, can be placed in whatever folder you want.
They could be in:
On the plus side, you get to put them wherever you want, even in multiple location. On the down side, there is no default static file directory.
Which means you'll have to keep track of the directory itself. The best way to store such information is in the web.config file of your application.
1. Add the config key in the <appSettings> of your web.config file.
<add key="StaticFileDirectory" value="MyStaticFiles" />
2. Add a reference to System.Configuration in your project.
It's listed under Assemblies > Framework.
3. Retrieve the config value.
string foldername = ConfigurationManager.AppSettings["StaticFileDirectory"];
4. Convert it to an absolute path.
string absoluteFolderPath = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, foldername);
5. Use it as you want to.
string absoluteFilePath = System.IO.Path.Combine(absoluteFolderPath, filename);
if (File.Exists(absoluteFilePath))

You can solve this issue by this way.
This function will search all subdirectories inside the given folder path ex:"C:\Users\NBY81HC\Desktop\hello" to return the file you want.
foreach (string file_path in Directory.GetFiles(#"C:\Users\NBY81HC\Desktop\hello", "test.txt", SearchOption.AllDirectories))
Console.WriteLine(file_path);//Return C:\Users\NBY81HC\Desktop\hello\aa\a2\test.txt


Adding content files to and accessing content files from a web service

I want to include an XML file in the bin directory of my web application/web service. I've included the bin folder in my project in Visual Studio and added the file. I set its Build Action to "Content" and Copy to Output Directory to "Copy Always". When my code goes to find it, I use
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!dir.EndsWith(#"\")) dir += #"\";
to get the directory and return it as appPath, where appPath is (unescaped version):
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vs\0c7655f\d4908928\assembly\dl3\5cf9fd67\fdc52284_ffa7d201\
then I append the file name of my XML file (where string filename = "myXmlFile.xml") to read it:
StreamReader reader = new StreamReader(appPath + filename);
But I get an exception on that line of code, that it could not find my file. I am handling for escaping of the slashes fine, so it is not that. When I checked the physical directory that the path points to, it was not copied to that directory, and that is the cause of the exception. So how do I get my file to get copied there?
How about using HostingEnvironment.ApplicationPhysicalPath
var dir = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "bin\\filename.xml")
See related question how to get the application path in

Read from a file not found in the startup project

My web solution has a number of projects.
The startup project is called: "Bob"
"Bob" has a reference to "Gill" project.
Inside "Gill", there is a file inside that i want to read. "Gill/One/Two/hi.txt"
I have some code in Bob, that calls some code inside the "Gill" project that tries to access the above txt file:
That does not find the file.
I have marked the file build as
I have marked Copy to Output Directory as:
Copy if newer
What's the approach to getting access to the file?
If you've set the file to Copy To Output Directory, this will be in the bin directory of your application.
Changing Igor answer slightly, you could do
var root = System.Web.Hosting.HostingEnvironment.MapPath("~/bin");
var filePath = System.IO.Path.Combine(root, "One", "Two", "hi.txt");
if(System.IO.File.Exists(filePath)) {
var allText = System.IO.File.ReadAllText(filePath);
Which will resolve the bin directory for your application, where your file will be rooted.
try file path as
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\One\\Two\\hi.txt");
And set
Build Action: none
Copy to output: Copy always (or copy if newer)
I believe the ~ is used by .NET webform and only be allowed by Server.MapPath (
You could get the File by new System.IO.FileInfo(#"One/Two/hi.txt");
You need to
Make sure the file has been copied to your web site. You cant read from it if it does not exist.
Put together the file path correctly. The executing code does not directly know about ~ as a path character, you have to translate that into a root path of the website for any types in the System.IO namespace like File or Path. The best way to do that is to use HostingEnvironment.
var root = System.Web.Hosting.HostingEnvironment.MapPath("~");
var filePath = System.IO.Path.Combine(root, "One", "Two", "hi.txt");
if(System.IO.File.Exists(filePath)) {
var allText = System.IO.File.ReadAllText(filePath);
Files marked as Content and Copied referenced in outside projects (that are in turn referenced by the web project) will have their content copied relative to the bin folder of the web application. In this case add bin to the root path like so:
var root = System.Web.Hosting.HostingEnvironment.MapPath("~/bin");

Includes directory in build path

I want to be able to add a directory containing a bunch of files to my project. This directory will need to be included in the build. I then need to be able to get the path of this directory at runtime.
Is there a way I can do this?
You can add the directory with all the files and set the property of each files as follows. This will make sure that all the files will be copied to running directory.
Build Action : Content
Copy Always : Copy Always (Depends on you need)
Access Files in the directory at run time
You can access using relelative path
bool isFileExisit = File.Exist("DirectoryName/FileName");
Access File with full path
String fullFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DirectoryName", "FileName");
Include the folder to the project, and set Copy to output directory property to true.
To reference application path use System.AppDomain.CurrentDomain.BaseDirectory
You need to add the files to your project and mark them as content files.
Then, to access them at run-time use the following code snippet:
string[] resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (var resourceName in resourceNames)
if(resourceName.StartsWith("<assembly short name>.<directory name>"))
// do something

How to get a path from a directory in a C# console application?

Say I have this file structure
Soultion-> Folder1 -> FileIwant.html
So this could be something like C:\Soultion\Folder1\FilterIwant.html
Now I need to read this file into my application. I can't just hardcode it since when I give it to someone else they might put it on F: drive or something.
Or when I create a msi file the path might be completely different. So how can I say maybe take
and use that to get the folder path regardless of where they put it?
I tried Path.GetFullPath but I land up in the bin/debug directory. But my file is not in that directory. I think it is a couple directories before. Also if I make a msi file will I have bin/debug directory?
Why is a file which is used as part of your application not in the same folder as the application? It sounds to me like you should set the properties on that file to copy to the output folder when you do a build.
Doing that will make sure your file is in the bin\debug folder.
either that or you should be placing your files in one of the special folders, app data or my documents spring to mind.
When Visual Studio compiles your project, it will be putting the output into the bin\debug directory. Any content files that you want to reference must also be copied to those locations, in order for your app residing in that directory to be able to read that file.
You have two choices:
either you set the Copy to Output Directory property on your FilterIwant.html to Copy if newer; in that case, if the file has changed, it will be copied to the output directory, and you should be able to reference it and load it there
you just define a path in your app.config, something like DataPath, and set it to your folder where the file resides. From your app, you then create the full path name for that file as Path.Combine(AppSettings["DataPath"], "FilterIwant.html") - with this approach, you become totally independant of where the file really is and you don't need to move around anything. Also: this gives you the opportunity to create an admin/config utility for your users later on, so that they can pick any directory they like, and your app will find those files there.
In my console app, I started with the debug directory until i found the closest parent folder I wanted.
static void Main(string[] args)
var debugDir = Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(debugDir);
var searchDir = "";
while (!di.FullName.ToLower().EndsWith("Folder1"))
if(di.FullName.ToLower().EndsWith(":")) //if you went too far up as in "D:" then
di = di.Parent;
You need the help of System.Io.Path class:
GetFullPath: Returns the absolute path for the specified path string.
You might also need the application directory - this is where your application will be installed:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
The bin/Debug path will not be present when you run your installed application (unless you specifically tell the installer to use that subdirectory, of course).
You probably want to pass the full path as a command line argument. You can then get the argument using the args parameter of the Main method. To convert a relative path to an absolute one you can use Path.GetFullPath:
using System;
using System.IO;
public class CommandLine
public static void Main(string[] args)
// The path is passed as the first argument
string fileName = arg[0];
// get absolute path
fileName = Path.GetFullPath(fileName);
// TODO: do whatever needs to done with the passed file name

Find Full Path by Folder Name for Folder in Another Project

In my web project solution I have numerous projects added in it.
I have folder names as 'Localization' in one of the project.
So by specifying folder name in code behind from another project, how do I get the full path of the folder.
If folder is present in one of the project then I need to get the full path.
You can use this example, for get the full path of another folder.
System.IO.Path.GetFullPath(System.IO.Path.Combine(Server.MapPath("~"), "../Localization/"));
You mention "if folder is present", so you will need to test to see if the folder is there first:
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/FolderName"));
if (dir.Exists)
//dir.FullName will get you the path.
//This is the same things as Server.MapPath("~/FolderName"), but that
//will return a path even if the folder isn't there.
//folder doesn't exist.
Since you are trying to go outside your project, you can do something like this (depending on your solution structure):
string path = Path.GetFullPath(Path.Combine(Server.MapPath("~"), #"..\OtherProjectName/FolderName"));
DirectoryInfo dir = new DirectoryInfo(path);