I have a asp.net website that has the following directory:
C:\Users\Desktop\Testing\src\website
I have another folder called "files" that is here:
C:\Users\Desktop\Testing\src\files
from inside my project i am trying to read files from the "files" folder, i am doing it like this:
var path = HttpContext.Current.Server.MapPath("/files");
I also tried :
var path = HttpContext.Current.Server.MapPath("..");
But it says Failed to map the path '/files'.
What could be the reason for this? could it do something with my IIS? How can i get this working??
Thank you!
You can't do this. The Server.MapPath method only works with folders relative to the root of the web application which in your case is C:\Users\Desktop\Testing\src\website. You cannot go one level up in the hierarchy using this method as you are leaving the domain of control of this ASP.NET application. To achieve this you will have to use an absolute path. For example if you want to read some file which is situated outside of your application:
var data = File.ReadAllText(#"C:\Users\Desktop\Testing\src\files\somefile.txt");
assuming the websites's virtual directory is mapped to .../src/website you need to get "files" folder like this:
var filesDir = Path.Combine(HttpContext.Current.Server.MapPath("~"), "../files/");
Related
When users upload an image, it is stored in the file system but outside of what is publicly reachable.
I am displaying a list of items, and each item has an image associated with it.
How can I display the image when the actual file is stored outside of the wwwroot folder? i.e. it isn't publicly available.
Since the action method is running on the server, it can access any file it has permission to. The file does not need to be within the wwwroot folder. You simply need to tell the action method which image to get.
For instance:
<img src="/mycontroller/myaction/123">
Your action would then look like:
public FileResult MyAction(int id)
{
var dir = "c:\myimages\";
var path = Path.Combine(dir, id + ".jpg");
return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
}
Please note that the id is an int which will prevent someone from injecting a path to access different files on the drive/share.
You could do this two ways.
Option 1, you could create a Virtual directory which points to this other Directory. This would then mean that you could access the images via another URL. e.g. Create a Virtual Directory called "OtherImages", then your URL would be;
http://www.mywebsite.com/otherimages/myimage.jpg
Option 2, you could create a simple HttpHandler which can load up the image from the absolute path, then output this in the response. Read up on HttpHandlers and dynamically generating images.
I want to get the parent directory path of my solution's startup project, by testing that code
string parent = System.IO.Directory.GetParent(Server.MapPath("~/"));
I get the directory where my solution's startup project is currently placed. Why ?
I am not sure why this happens, at the momemt. But you can do
string parent = new DirectoryInfo(Server.MapPath("~/")).Parent.FullName;
to get the parent directory path.
I try to find a answer why System.IO.Directory.GetParent(Server.MapPath("~/")) does not work and update this if i found something.
Update
I found a possible answer on another Stackoverflow question who GSerg say
I can only assume Directory.GetParent(...) can't assume that C:\parent\child is a directory instead of a file with no file extension. DirectoryInfo can, because you're constructing the object that way.
The reason this is happening is because Server.MapPath is appending a \ at the end of the path (even if you remove it from your MapPath), for example:
C:\foo\bar\
If you try to get the parent directory of that, it will give C:\foo\bar without the slash.
So this will work:
var path = System.IO.Directory.GetParent(Server.MapPath("~").TrimEnd('\\'));
Here is an alternative:
var path = new System.IO.DirectoryInfo(Server.MapPath("~")).Parent.FullName;
I devloped a custom rule editor able to create drl file and save them in file system under a given directory. (e.g. c:\savedRules\rule.drl).
The problem is that once the rule is saved I need to run it with drools engine.
In my class I try to load rule in this way:
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("c:\savedRules\rule.drl"), ResourceType.DRL);
but its doesn't work. the exception is "rule.drl cannot be opened because it does not exist" but it actually exists....
What am I doing wrong? Is there another way to load rules directly from file system?
Try using,
FileInputStream fis = new FileInputStream(drlFile);
kbuilder.add(ResourceFactory.newInputStreamResource(fis), ResourceType.DRL);
Thanks.
kbuilder.add(ResourceFactory.newClassPathResource("LoopConditionRules.drl"),ResourceType.DRL);
Just add this line and copy your drl file in resource folder of the project, when you will run it will automatically find the file from the project there is no need to give specific path for your file.
Try this way, may be you can get your required result.
Try the below code, this will work.
kbuilder.add(ResourceFactory.newFileResource(drlFileName), ResourceType.DRL);
Is it possible to put resource files (.resx) within subfolders inside App_GlobalResources?
For example:
/App_GlobalResources/someresources/myfile.resx
/App_GlobalResources/someresources/myfile.fr-fr.resx
/App_GlobalResources/othereresources/otherfile.resx
/App_GlobalResources/othereresources/otherfile.fr-fr.resx
Or, are all the .resx files placed directly inside App_GlobalResources?
If it is possible to use subfolders, how do you programmatically access resources within subfolders?
Technically, yes it is possible but there are some pitfalls. First, let me show you an example. Suppose my App_GlobalResources folder looks like so:
/App_GlobalResources
/Test
TestSubresource.resx
TestResource.resx
Each resource file has a single entry called "TestString". I added each resource file using the Visual Studio menu so it created a class for me. By default, all classes added to the App_GlobalResources folder will have the same namespace of Resource. So, if I want to use the class generator and I want Test in the namespace, I need to go into the TestSubresource.Designer.cs file and manually change the namespace. Once I do that, I can do the following:
var rootResource = Resources.TestResource.TestString;
var subResource = Resources.Test.TestSubResource.TestString;
I can also reference them using GetGlobalResourceObject:
var rootResource = GetGlobalResourceObject( "TestResource", "TestString" );
var subResource1 = GetGlobalResourceObject( "TestSubresource", "TestString" );
Notice that I still use the "TestSubresource" as the means to reference the resources in that file even though it is in a subfolder. Now, one of the catches, is that all the files must be unique across all folders in App_GlobalResources or your project will throw a runtime error. If I add a resource named "TestResource.resx" to /Test, it will throw the following runtime error:
The resource file '/App_GlobalResources/TestResource.resx' cannot be
used, as it conflicts with another file with the same name.).
This is true even if I change the namespace on the new resource.
So, in conclusion, yes it is possible, but you increase the odds of getting a runtime error because of two identically named resource files in different parts of the App_GlobalResources folder structure which is allowed by the file system but not by .NET.
It's possible. At least, I managed to do it.
Within a web site I added the App_GlobalResources folder. Inside it I created another folder "MyFolder" and placed MyResource.resx file inside. Resx file contained one pair MyKey1 - MyValue1.
Using the GetResource method of the following class I successfully extracted "MyValue1" for name="MyKey1"
static class Class1 {
static Assembly FindGlobalResAssembly() {
foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) {
if(asm.FullName.StartsWith("App_GlobalResources."))
return asm;
}
return null;
}
public static object GetResource(string name) {
Assembly asm = FindGlobalResAssembly();
if(asm == null)
return null;
return new ResourceManager("Resources.MyResource", asm).GetObject(name);
}
}
This approach works in Medium trust also.
It seems that folders make no difference when accessing resources from code.
How may I know File.nativepath from the folder that my .app or .exe AIR app is running?
When I try this I just get
'/Users/MYNAME/Desktop/MYAPP/Contents/Resources/FILETHATINEED.xml'
I need put this on any folder and read a xml file in the same folder. I don't need my xml file inside the package.
I need this structure
/folder/AIRAPP.exe
/folder/FILE.xml
Thanx in advance.
From what I can find there is no way to get that without doing some work yourself. If we assume that the File.applicationDirectory points to the wrong place only on Mac (which seems like the case), we can do this:
var appDir = File.applicationDirectory
if ( appDir.resolvePath("../../Contents/MacOS").exists ) {
appDir = appDir.resolvePath("../../..");
}
That is, check if the parent directories of the app directory match the Mac .app bundle directory structure, and in that case use the parent's parent's parent (which should then be the directory containing the .app bundle).
I believe you want to use File.applicationDirectory.