I tried to create a URI like this
Uri uri = new Uri("file:///Pages/Menu/Page.xaml");
So I have a WPF project and its path is like this: C:/Programing/MyProjectName/
Now the path to my file are C:/Programing/MyProjectName/Pages/Menu/Page.xaml
as you saw above the path I give to the Uri is file:///Pages/Menu/Page.xaml when I run this I get the error can't find the part of the path C:/Pages/Menu/Page.xaml which I expect because as I said above my files path is C:/Programing/MyProjectName/Pages/Menu/Page.xaml
so my question is, is there any way to use a relative path in my Uri, I want to use a relative path because my app will not always be in the same folder.
Incase your wondering why I'm using a Uri is because I'm setting the Source property on a WPF frame and it uses Uri instead of string.
In order to load a Page, use a Resource File Pack URI:
var uri = new Uri("pack://application:,,,/Pages/Menu/Page.xaml");
Which could also be written like this:
var uri = new Uri("/Pages/Menu/Page.xaml", UriKind.Relative);
Related
Is there another way to get the root of a wpf application as a string?
Now I'm still using
string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));
This gives me the root as a string, but I assume this is not the right way to do it.
I also tried
string txt = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
but this sends me to root/bin/debug.
I only need the root as a string
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Another way is:
Environment.CurrentDirectory
if it was not changed.
You can find the file path of the root folder of the startup project in a WPF App like this:
string applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string rootPath = Directory.GetParent(applicationDirectory).Parent.FullName;
Or to complete your example by getting the file path of the parent folder of the parent folder, you can do this:
string rootPath = Directory.GetParent(txt).Parent.FullName;
UPDATE >>>
In order to access your project Images folder, you can do this:
Path.Combine(Directory.GetParent(applicationDirectory).Parent.FullName, "Images");
You should put the images in a folder of your Visual Studio project called "Images" and set their Build Action to Resource (as shown here).
If you then get a relative image path from your DB, you would create a Pack URI and load a BitmapImage like this:
var imagePath = "Images/SomeImage.jpg"; // actually from DB
var uri = new Uri("pack://application:,,,/" + imagePath);
var bitmap = new BitmapImage(uri);
I am making a simplePlayer WindowsForm. In order for the video to play i need to provide the url
axWindowsMediaPlayer1.URL = #"C:\Users\Stephan\Desktop\trasa-1250.wmv";
Now i need to either use relative paths or add them as a resource and get the url for that resource but i don't know how to do that:
wplayer.URL = Resources.trasa_1250.
I've tried using
axWindowsMediaPlayer1.URL = #"~\trasa-1250.wmv";
and
axWindowsMediaPlayer1.URL = #".\trasa-1250.wmv";
but printing #"~\trasa-1250.wmv"; and #.\trasa-1250.wmv"; prints them as they are without replacing the ~ or the .
To get the absolute path for a file, by supplying a name relative to the current directory, you can use:
string filename = "trasa-1250.wmv";
string path = Path.GetFullPath(filename);
For completeness sake, to create an actual Url from this:
string url = new Uri(path).AbsoluteUri;
You can't create a Url to an embedded resource, unless you program the player to accept a custom Url scheme (to allow, for instance, "resource://assemblyName.namespace.resourceName") and process it correctly.
A common alternative is to let the caller provide the Stream from which to read the media - and let them decide on how to access the resource.
Here is a tutorial that will help:
https://www.youtube.com/watch?v=nF-HYoTurc8
You could also get the URL like this:
Uri MyUri = new Uri(#"/Resources/trasa-1250.wmv", UriKind.Relative);
I have a image-path that must be inserted into following code:
Uri uri = new Uri(#"C:\Users\user\Desktop\dir\private- name\Easy\Easy_v\Easy_v\Attempts\image.jpg");
BitmapImage img = new BitmapImage(uri);
When I need to change this path, I'm trying to get path with a function I've written, but when I inserted the full path into the uri, this character: \ has been replaced with /
Any way to get the correct image path, and replace it with the one above?
Uri class has a property called
uri.LocalPath
this is the fullpath name expressed according to the local system preferences
of course you can apply the normal Path methods to this property
Console.WriteLine(Path.GetDirectoryName(uri.LocalPath));
See MSDN docs
I need use images in 2 .NET assemblies. One is WPF app {myApp.exe} and second is *.dll {myDll.dll}. File are located in this file structure:
**AppFolder** consist these files and one subfolder(avatars):
-myApp.exe
-myDll.dll
-avatars {folder} consist:
-manImages.jpg
-womanImages.jpg
I try user uri in this format
new Uri(#"C:\Users\avatars\manImages.jpg", UriKind.RelativeOrAbsolute);
but this format does not work, images are empty.
I would expect
new Uri(#"avatars\manImages.jpg", UriKind.RelativeOrAbsolute);
to work?
You could also try:
new Uri(Environment.CurrentDirectory + #"\avatars\manImages.jpg", UriKind.RelativeOrAbsolute);
On the dll side you can write this:
string imgPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "avatars\\manImages.jpg");
I know next to nothing about the URI class. I need to make an Relative URI to reference a file in my project (that is on the root of the project).
How can I do that?
This is what I have tried and it does not seem to work:
var uri = new Uri("ModuleCatalog.xaml", UriKind.Relative)
I have also tried:
var uri = new Uri("/ShellProject;component/ModuleCatalog.xaml", UriKind.Relative);
When I examine my uri variable in the debugger it has thrown a lot of exceptions. The only value that is real is the original text value.
What do I need to do to make it a valid uri?
I think that I don't get how a Uri object works.
I guess I am asking for the basics of how to make a uri and have it reference a file in my project (with out having to hard code the full path from the C:\ drive.
Is your application a Web App?
You don't use URIs to reference local files in non-web apps.
The expression
var uri = new Uri("ModuleCatalog.xaml", UriKind.Relative)
Doesn't throw any exceptions on construction, it throws later when it is used improperly.
Since you mentioned you are developing a WPF app, if you want to locate this file you should use:
string assemblyLocation = Assembly.GetExecutingAssembly().Location
string moduleCatalogPath = Path.Combine(assemblyLocation, "ModuleCatalog.xaml");
What does "not work" mean? Both code examples compile and run, giving valid relative URIs.
Or perhaps you want to make absolute URIs from the relative URIs? That is, if your root is "http://example.com", you want to create "http://example.com/ModuleCatalog.xaml". In that case, use the Uri.TryCreate overload that lets you pass in the root. For example:
Uri baseUri = new Uri("http://example.com");
Uri newUri;
if (Uri.TryCreate(baseUri, "ModuleCatalogue.xaml", out newUri))
{
// Uri created successfully
}