URI Class 101 - Relative Project Reference - c#

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
}

Related

How to create a relative path using System.Uri

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);

Libragnar(Libtorrent Wrapper) LocalTorrent File, Instead of URL? C#/C++

Question:
Does anyone know how to add a torrent to LibRagnar using a filepath to a torrent, instead of a Url? (LibRagnar is a libtorrent Wrapper)
libragnar = C#
libtorrent = C++
Alternatively if anyone knows How I can use Libtorrent To add the torrent to a session, But use a local file (Whilst still controlling Everything else using Libragnar).But I am not sure where to start with Libtorrent.
Reason For Problem:
I have to use a filepath because the Torrent Requires cookie login to access it. So I either Need to get Libragnar to use a CookieCollection when getting a torrent from a URL or make it use a local ".torrent" file.
Problem:
I am currently trying to use a filepath instead of URL and the Torrent Status gives an error:unsupported URL protocol: D:\Programming\bin\Debug\Tempfiles\File.torrent. Which wont allow me to start it.
Example:
var addParams = new AddTorrentParams
{
SavePath = "C:\\Downloads",
Url = "D:\\Programming\\bin\\Debug\\Tempfiles\\File.torrent"
};
Edit: Answer from Tom W (Posted in C# Chatroom)
var ati = new AddTorrentParams()
{
TorrentInfo = new TorrentInfo("C:\thing.torrent"),
SavePath = #"C:\save\"
};
Note About Answer: I attempted to edit Tom W's post and add the answer he gave me in the Chatroom, However I guess it got declined? But Since he was the one who helped me I wanted him to get credit, and also wanted anyone else having this issue, to have an answer. So I had to add the answer to the bottom of my question.
From the libtorrent documentation it appears that:
The only mandatory parameters are save_path which is the directory
where you want the files to be saved. You also need to specify either
the ti (the torrent file), the info_hash (the info hash of the
torrent) or the url (the URL to where to download the .torrent file
from)
Libragnar's AddTorrentParams appears to be a wrapper around add_torrent_params and has a property called TorrentInfo. I suspect if you avoid setting the URL, and set this property to an instance of TorrentInfo instead, you ought to get the result you want.
Disclaimer: I've never worked with torrents before, don't know this library, and don't work in C++.

How to get URL of a resource in visual studio c#?

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);

Combining System.IO.Path and System.Uri into a complete system path

Im writing a small web interface for my application and am getting hung on combining a known docs path and the request URI into a absolute local path to retrieve the file from.
For simplicity sake I narrowed my code down to the relevant:
string AssemblyDirectory = "C:\MyAppDir\";
string uri = "/index.html";
return new Uri(new Uri(Path.Combine(AssemblyDirectory, "http_docs")), uri).AbsolutePath;
This will always just return the uri portion only; ie the return is "/index.html", it does not seem to like to combine types of Uri and Path properly. I am aware that simply replacing the "/"'s with "\"'s then doing a simple path.combine would work fine, but I cant help but think there is a .net solution to this somewhere that I am overlooking.
The matter is that you have a / before your index.html. It makes the Uri API to refer to the root of the uri, which gives you at last : c:\index.html.
Remove the / in uri to make it work :
string uri = "index.html";

ASP.NET MVC Get file from virtual path

For various reasons, in development I occasionally want to intercept a request for, say, ~/MyStyle.css
What I want to do is make the following snippet work:
string absFile = VirtualPathUtility.ToAbsolute(file);
return System.IO.File.ReadAllText(absFile);
This absolute path is absolute for the webserver though, it's not going to map to "C:\whatever". Is there an equivalent method to go to the file system? (Or a ReadFromVirtualPath etc.?)
Use Server.MapPath() to get the file system path for a requested application path.
string absFile = Server.MapPath(file);
or
string absFile = HttpContext.Current.Server.MapPath(file);
You can also use the OpenFile method on VirtualPathProvider to get a Stream pointing at your file
var stream = HostingEnvironment.VirtualPathProvider.OpenFile(file);
var text = new StreamReader(stream).ReadToEnd();
Generally this approach is preferable since you can now, at a later point implement a VirtualPathProvider where, lets say all your css files where located in a database.

Categories

Resources