absolute File path to relative file path - filepath

I have a class library project (D:/projectName/SampleClassPrj) and a console application project D:/projectName/ConsolePrj.
In class library project, a json file test.json and a class sample.cs (read and deserialise json) both present.
d:/projectName/SampleClassPrj/test.json
d:/projectName/SampleClassPrj/sample.cs
This works with absolute path like,
d:/projectName/SampleClassPrj/test.json
but not working with relative path.
with relative path, by using file not found error as it is trying to find the file in the console (running) application assembly folder.
D:/projectName/ConsolePrj/bin/test.json
please help to find the solution

first, check json file property "copy if newer".
then, use Appdomain.CurrentDomain.BaseDirectory
string result = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ConnectionString.txt"));

var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\")));
StreamReader r = new StreamReader(Path.Combine(path, "sample.json"))
for .NET core 3.1
This worked for me

Related

what is absolute path in C++/WinRT

the error: WinRT originate error-0x80070057:'The specified path (msappx:\Local\C:\Windows\Web\Screen\img103.png) is not an absolute path, and relative paths are not allowed. '
when I used C++/WinRT,the function call is
winrt::Windows::Foundation::IAsyncOperationwinrt::Windows::Storage::StorageFile temp = StorageFile::GetFileFromPathAsync(hFilname);
what can I do?
For StorageFile.GetFileFromPathAsync() method, you don't have permission to access the specified file, please see here.
My suggestion is that you could store the image file in Application install directory that app can access, then use the following code to access it.
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
string path= Path.Combine(folder.Path, #"Data\img1.png");
StorageFile.GetFileFromPathAsync(path);
Note that the Application install directory is ..ProjectPath\bin\x86\Debug\Appx

JavaFX jar doesnt read .properties file

I have developed a javafx application that reads the database configuration properties from a ".properties file".When i run the app in eclipse everything works fine.The problem is when running the app from the generated executable jar,it throws me a NullPointerException because it cant read the ".properties file".
The code is :
FileInputStream fis=new FileInputStream("resources/META-INF/db/db.properties");
Properties properties = new Properties();
properties.load(fis);
I searched about this and i saw some examples of using InputStream :
Properties pp = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("errors.properties");
pp.load(is);
But still doesnt work.Any idea?strong text
Try creating a folder that you always have with you Jar. The folder name should be resources. This folder should have a folder named META-INF. META-INF should have a folder named db. Finally, db should have the db.properties file in it.

Image as resource in Asp.Net 5 class library

In .net 4.5 i was able to add a resource file to my project, add images as bitmaps to this resource file and access them by Properties.Resources.ImageName. How do i compile images in 4.6 dnx?
Thanks in advance!
You can specify the files that will be compiled into the assembly under the "resources" section in project.json, like so:
"resources": [
"path/to/yourfile.png"
],
After which, assuming your project's name is YourProject, the file can be accessed via:
const string path = "YourProject.path.to.yourfile.png";
Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
Note how the slashes in the filesystem path are converted into periods in the resource path.
Note: In 1.0.0-rc1 (and possibly -beta8, haven't checked) the project setting is renamed from resources to resource

Setting Jetty resourcebase to static file embedded in the same jar file

I am trying to access static resource (eg. first.html) packed inside the same .jar file (testJetty.jar), which also has a class which starts the jetty (v.8) server (MainTest.java). I am unable to set the resource base correctly.
The structure of my jar file (testJetty.jar):
testJetty.jar
first.html
MainTest.java
==
Works fine on local machine, but when I wrap it in jar file and then run it, it doesn't work, giving "404: File not found" error.
I tried to set the resourcebase with the following values, all of which failed:
a) Tried setting it to .
resource_handler.setResourceBase("."); // Results in directory containing the jar file, D:\Work\eclipseworkspace\testJettyResult
b) Tried getting it from getResource
ClassLoader loader = this.getClass().getClassLoader();
File indexLoc = new File(loader.getResource("first.html").getFile());
String htmlLoc = indexLoc.getAbsolutePath();
resource_handler.setResourceBase(htmloc); // Results in D:\Work\eclipseworkspace\testJettyResult\file:\D:\Work\eclipseworkspace\testJettyResult\testJetty1.jar!\first.html
c) Tried getting the webdir
String webDir = this.getClass().getProtectionDomain()
.getCodeSource().getLocation().toExternalForm();
resource_handler.setResourceBase(webdir); // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty1.jar
None of these 3 approaches worked.
Any help or alternative would be appreciated
Thanks
abbas
The solutions provided in this thread work but I think some clarity to the solution could be useful.
If you are building a fat jar and use the ProtectionDomain way you may hit some issues because you are loading the whole jar!
class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
So the better solution is the other provided solution
contextHandler.setResourceBase(
YourClass.class
.getClassLoader()
.getResource("WEB-INF")
.toExternalForm());
The problem here is if you are building a fat jar you are not really dumping your webapp resources into WEB-INF but are probably going into the root of the jar, so a simple workaround is to create a folder XXX and use the second approach as follows:
contextHandler.setResourceBase(
YourClass.class
.getClassLoader()
.getResource("XXX")
.toExternalForm());
Or change your build tool to export the webapp files into that given directory. Maybe Maven does this on a Jar for you but gradle does not.
Not unusually, I found a solution to my problem. The 3rd approach mentioned by Stephen in Embedded Jetty : how to use a .war that is included in the .jar from which Jetty starts? worked!
So, I changed from Resource_handler to WebAppContext, where WebAppContext is pointing to the same jar (testJetty.jar) and it worked!
String webDir = MainTest.class.getProtectionDomain()
.getCodeSource().getLocation().toExternalForm(); ; // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty.jar
WebAppContext webappContext = new WebAppContext(webDir, "/");
It looks like ClassLoader.getResource does not understand an empty string or . or / as an argument. In my jar file I had to move all stuf to WEB-INF(any other wrapping dir will do). So the code looks like
contextHandler.setResourceBase(EmbeddedJetty.class.getClassLoader().getResource("WEB-INF").toExternalForm());
so the context looks like this then:
ContextHandler:744 - Started o.e.j.w.WebAppContext#48b3806{/,jar:file:/Users/xxx/projects/dropbox/ui/target/ui-1.0-SNAPSHOT.jar!/WEB-INF,AVAILABLE}

How to get the uploaded file path?

I am using input tag type="file" to browse the file in asp.net.
I browsed the file "Linq2sql.zip" from the location "c\Desktop\Karthik\Linq2sql.zip".
i can get the file name and path using
HttpPostedFileBase file;
var filePath = Path.GetFullPath(file.FileName);
But File path is like = C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\DevServer\\10.0\\Linq2sql.zip
i have to get the original file path c\\Desktop\\Karthik\\Linq2sql.zip. How can i get?
You can not get the original path of the file on the client system; that information is not sent by the client.
The reason you get what you do with GetFullPath is because that forces a resolution with the simple file name alone with the current directory of the asp.net process. That info is utterly meaningless - and in fact incorrect - in this case.

Resources