How to get URL of a resource in visual studio c#? - 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);

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

C# Absolute URI removes ".." from URL

I have to upload a file via FTP to ftp://ftp.remoteServer.com
My root directory on remoteServer contains an "upload" and a "download" folder. I need to put my file in the "upload" directory. But on log in, the server automatically puts me in the "download" folder.
I tried doing this:
string serverTarget = "ftp://ftp.remoteServer.com/";
serverTarget += "../upload/myfile.txt";
Uri target = new Uri(serverTarget);
FTPWebRequest ftp = (FTPWebRequest)FtpWebRequest.Create(target);
using(Stream requestStream = ftp.GetRequestStream()) {
// Do upload here
}
This code fails with: (550) File unavailable (e.g., file not found, no access)
I debugged the code, and target.AbsoluteUri returns as ftp://ftp.remoteServer.com/upload instead of ftp://ftp.remoteServer.com/../upload (missing the ..)
If I put ftp://ftp.remoteServer.com/../upload in a browser, I can log in and verify this is the correct place where I want to put my file.
How can I get the FTPWebRequest to go to the correct place?
I believe you can encode the dots as %2E to keep the dots in your URI.
So something like:
string serverTarget = "ftp://ftp.remoteServer.com/%2E%2E/upload/myfile.txt";
Try this:
string serverTarget = "../upload/myfile.txt";
Uri uri = new Uri(serverTarget, UriKind.Relative);
Andy Evans' comment is correct.
Consider the URI: http://ftp.myserver.com/../. The .. means, "take me to the parent of this directory". But there is no parent! So when you derive the absolute URL, you're going to end up with http://ftp.myserver.com/ There is nothing else that the parser can do.
I think the problem is with the configuration of your FTP server. I assume that the directory structure looks something like:
ftproot
upload
download
It looks like the FTP service is automatically logging you to /ftproot/download. That is, the URI ftp.myserver.com gets mapped to /ftproot/download on the local file system. If that's the case, no amount of fiddling with the URI is going to get you anywhere. If the URI root is mapped to the download directory, there is no way you can, using the .. syntax, "go up one level and then down."
Are you able to upload using an FTP client such as Filezilla, or perhaps the Windows FTP command line tool? If so, what are the steps you take to do it? Can you make your code do the same thing?

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.

URI Class 101 - Relative Project Reference

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
}

How do I stream videos from web server in Silverlight using ExpressionMediaPlayer control?

I would like to stream videos that reside at the webserver from within a ExpressionMediaPlayer control. The following results in a network error. I believe that the problem is with my Uri. I have the videos inside the 'ClentBin' folder. Can anyone tell me how this is done?
private void videoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedVideo = (Video)videoList.SelectedItem;
PlaylistItem item = new PlaylistItem();
item.MediaSource = new Uri(#"/ClientBin/" + selectedVideo.FilePath, UriKind.RelativeOrAbsolute);
item.IsAdaptiveStreaming = false;
ep.Playlist.Items.Add(item);
}
Thanks!
There can be a number of factors that contribute to a network error in the Expression Media Player. Here are some basic checks...
1. Check the video file itself
Launch Windows Media Player, go to File > Open URL... and make sure you can play the video with the absolute URL, just to rule out any basic problems with the web server. (Note that this does not apply if you are working with Adaptive Streaming, which it doesn't appear you are.)
2. What does selectedVideo.FilePath contain?
Is this a simple file name (i.e. MyVideo.wmv) or is it a relative file path? Forward or backward slashes?
3. Try it with an absolute static URI
Just to rule out relative path issues with your app / web server / any virtual directory configuration, try:
item.MediaSource = new Uri(#"http://mysite.com/ClientBin/MyVideo.wmv", UriKind.Absolute);
4. Remove the leading slash from /ClientBin/
Try just new Uri(#"ClientBin/" + selectedVideo.FilePath, UriKind.Relative); and see if the relative path is then correct.

Categories

Resources