what is the servlet equivalent of Server.MapPath? - c#

I've got a folder in my Web Application, fonts. I'd like to get the path for each of those files in that directory. How do I do that? In asp.net I'd do something like:
System.IO.Directory.GetFiles(Server.MapPath("/fonts"))

String path = ServletContext.getRealPath("/fonts");
Javadoc.

You can use method getResourcePaths(String path) from ServletContext class for this purpose. It will return Set with directory-style listing of resources for specified (web-application mapped) path.
If you want to read content of file specified by mapped path, you can use method getResourceAsStream() from ServletContext returning InputStream for specified resource.

java.io.File dir = new java.io.File("/fonts");
String[] files = dir.list();

Related

Is there a method to manage relative paths in C#? [duplicate]

I have xml files that contain href file paths to images (e.g. "....\images\image.jpg"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images and turn them into absolute paths in the file system.
I know about the GetFullPath method, but I tried it and it only seems to work from the CurrentDirectory set, which appears to be C: so I don't see how I could use that. And still, I have the absolute path of the file containing the hrefs, and the href relative paths, so since it is a simple task for me to count back the number of "....\" parts based on the absolute path of the containing file, it seems there must be a way to do this programmatically as well.
I'm hoping there's some simple method I just don't know about! Any ideas?
string exactPath = Path.GetFullPath(yourRelativePath);
works
Assuming you know the real directory the XML file lives in use Path.Combine, e.g.
var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");
If you want to get back the full path with any ..'s collapsed then you can use:
Path.GetFullPath((new Uri(absolute_path)).LocalPath);
This worked.
var s = Path.Combine(#"C:\some\location", #"..\other\file.txt");
s = Path.GetFullPath(s);
It`s best way for convert the Relative path to the absolute path!
string absolutePath = System.IO.Path.GetFullPath(relativePath);
You can use Path.Combine with the "base" path, then GetFullPath on the results.
string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, #"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath); // Will turn the above into a proper abs path
Have you tried Server.MapPath method. Here is an example
string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg
This worked for me.
//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat";
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
Take a look at Path.Combine
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx

Get the filepath of a static file to work local and on server

I want to provide a new file needed for configuring my application - here it's the settings.json:
How do I get the correct path of the file?
I tried:
System.Reflection.Assembly.GetExecutingAssembly().Location // gets a DLL somewhere /Temporary ASP.NET Files/
but when I publish it to a customers server the location is next to the web.config like this (and is a different path):
Any idea?
You can use AppDomain.CurrentDomain.BaseDirectory to get base directory of your application.
Something like this should work:
string settingsJson = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
You can try one of these two methods:
string path = System.IO.Directory.GetCurrentDirectory();
string path = Environment.CurrentDirectory;
Path.Combine(path, "settings.json");

Getting DLL's 'Content' file from web app

I have a class library that has some text files with Build Action = Content. These text files are read by a function within the class library. To get the location of the text files I use this:
var filePath = System.IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"AFolder",
"textFileName.txt");
var fileContent = System.IO.File.ReadAllText(filePath);
I have successfully retrieved the content of the text file when I called the function from a unit test. The text file is in C:\MySolution\MyProject.Test\bin\Debug\AFolder\textFileName.txt.
I have another web app that references this class library. When I called the function to read the text file from the web app, it couldn't find the text file because it tried to get the file from C:\MySolution\MyProject.Web\AFolder\textFileName.txt while the text file is actually in C:\MySolution\MyProject.Web\bin\AFolder\textFileName.txt.
So my problem is calling AppDomain.CurrentDomain.BaseDirectory doesn't always work. What should I use instead to get the folder location?
Found the answer from the link on the right by using System.Reflection.Assembly.GetExecutingAssembly().CodeBase:
var libPath = System.IO.Path.GetDirectoryName(
new Uri(
System.Reflection.Assembly.GetExecutingAssembly().CodeBase
).LocalPath
);
var filePath = System.IO.Path.Combine(
libPath,
"AFolder",
"textFileName.txt");
var fileContent = System.IO.File.ReadAllText(filePath);
You can try System.WebHttpRuntime.BinDirectory to get the physical path to the /bin directory for the current application.
EDIT 1:
Another way to get without using System.Web is using AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, this property provides list of directories under the application base directory that are probed for private assemblies.

Relative path to absolute path in C#?

I have xml files that contain href file paths to images (e.g. "....\images\image.jpg"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images and turn them into absolute paths in the file system.
I know about the GetFullPath method, but I tried it and it only seems to work from the CurrentDirectory set, which appears to be C: so I don't see how I could use that. And still, I have the absolute path of the file containing the hrefs, and the href relative paths, so since it is a simple task for me to count back the number of "....\" parts based on the absolute path of the containing file, it seems there must be a way to do this programmatically as well.
I'm hoping there's some simple method I just don't know about! Any ideas?
string exactPath = Path.GetFullPath(yourRelativePath);
works
Assuming you know the real directory the XML file lives in use Path.Combine, e.g.
var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");
If you want to get back the full path with any ..'s collapsed then you can use:
Path.GetFullPath((new Uri(absolute_path)).LocalPath);
This worked.
var s = Path.Combine(#"C:\some\location", #"..\other\file.txt");
s = Path.GetFullPath(s);
It`s best way for convert the Relative path to the absolute path!
string absolutePath = System.IO.Path.GetFullPath(relativePath);
You can use Path.Combine with the "base" path, then GetFullPath on the results.
string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, #"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath); // Will turn the above into a proper abs path
Have you tried Server.MapPath method. Here is an example
string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg
This worked for me.
//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat";
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
Take a look at Path.Combine
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx

How to get a part from the full path in C#?

I have a full path as given below.
C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd
How can the DTDs "part" to be fetched from this whole part?
Desired output:
C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug‌​\DannyGoXuk.DTDs
Can I use String's methods for this?
If yes, then how to fetch it?
Use System.IO.Path.GetDirectoryName() for the entire path, or new DirectoryInfo(path).Parent.Name for just the name of that one folder.
There is no directory named "DTDs" in the path you posted. IT looks like there's a file named "DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd", but the periods (.) in that path are not valid directory separator characters. Did you mean "DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd"?
If that's the case, given that entire new path, you want something like this to return a list of files in the DTDs folder:
string path = #"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd";
string[] files = new DirectoryInfo(path).Parent.GetFiles();
in properties window i choose Build Type as Embedded resource.
And now we finally get to it. When you choose "Embedded Resource", the item is bundled into your executable program file. There is no direct path anymore. Instead, set your Build Type to "Content" and set "Copy to Output Directory" to "Copy Always" or "Copy if Newer".
Calling
System.IO.Path.GetFileName
with the full directory path returns the last part of the path which is a directory name. GetDirectoryName returns the whole path of parent directory which is unwanted.
If you have a file name and you just want the name of the parent directory:
var directoryFullPath = Path.GetDirectoryName(#"C:\DTDs\mydtd.dtd"); // C:\DTDs
var directoryName = Path.GetFileName(directoryFullPath); // DTDs
You can also use Directory to get the directory from the full file path:
Directory.GetParent(path).FullName
Edit: Please read the OP’s question and all of her comments carefully before downvotiong this. The OP’s title question isn’t EXACTLY what she wanted. My answer gave her what she needed to solve her problem. Which is why she voted it the answer. Yes, Joel’s answer is correct if specifically answering the title question. But after reading her comments, you’ll see that not exactly what she was looking for. Thanks.
Use this ...
string strFullPath = #"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd";
string strDirName;
int intLocation, intLength;
intLength = strFullPath.Length;
intLocation = strFullPath.IndexOf("DTDs");
strDirName = strFullPath.Substring(0, intLocation);
textBox2.Text = strDirName;
System.IO.Path.GetFileName( System.IO.Path.GetDirectoryName( fullPath ) )
That will return just the name of the folder containing the file.
For
C:\windows\system32\user32.dll
this will return
system32
I'm inferring that that's what you want.
Use:
string dirName = new DirectoryInfo(fullPath).name;
You can use:
System.IO.Path.GetDirectoryName(path);
You can use Path ...
Path.GetDirectoryName(myStr);
Use the FileInfo object...
FileInfo info = new FileInfo(#"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
string directoryName = info.Directory.FullName;
The file doesn't even have to really exist.
Don't use string manipulation directly. Instead use GetDirectoryName of the Path class:
System.IO.Path.GetDirectoryName(myPath);
Path.GetDirectory on the path you have specified returns:
"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug"
Try it yourself:
var path = Path.GetDirectoryName(#"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
Your question is a little strange though- there is no directory called DTDs.

Categories