I feel really stupid but i cant figure this one out.
This works flawless;
((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri("http://www.google.com"));
But when i try to navigate to a file on disk it fails
string path =#"D:\dev\MySite.html";
((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri(path));
I guess i cant use Uri but what else should i use to navigate to a file on disk?
full code;
private void webControlAvailable(object sender, ControlAvailableEventArgs e)
{
string path =#"D:\dev\MySite.html";
((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri(path));
}
Using the System.Windows.Controls.WebBrowser to navigate to local files is quite a tough one. You can try this one, though:
string path =#"file://127.0.0.1/D$/dev/MySite.html";
((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri(path));
If you have JavaScript one your site it might be a little ugly and fail.
You can also use a System.IO.Stream to navigate, using WebBrwoser.NavigateToStream(), see this thread on SO and the official documentation.
Related
I'm developing a retro-style game in C# .NET-Framework, and I would like to use different sounds and music in my game. But I have a problem. The original System.Windows.Media.SoundPlayer doesn't support 2 (or more) sounds being played. When one starts, it stops the active one.
I'm looking for a solution that can play different audio at the same time. I tried threading the different SoundPlayers to different threads, but that wasn't a good solution for me (+ it didn't work).
I read about System.Windows.Media.MediaPlayer, and the different controls that you could use with it. This instantly had me interested, especially after I read that you could play different sounds at once.
But trying to use the MediaPlayer in my game, it throws an error, because the URL isn't spelled correct. Here is my code:
using System.Windows.Media;
MediaPlayer Sound = new MediaPlayer();
MediaPlayer BackgroundMusic = new MediaPlayer();
private void Form1_Load(object sender, EventArgs e)
{
Sound.Open(new Uri("Text1.wav"));
BackgroundMusic.Open(new Uri("BackgroundMusicMix.wav"));
}
private void BtnSound_Click(object sender, EventArgs e)
{
Sound.Play();
}
private void BtnBackgroundSound_Click(object sender, EventArgs e)
{
BackgroundMusic.Play();
}
The .wav-files are here located in the \bin\debug folder of my solution, because the SoundPlayer also gets it's sounds from there. I am aware of the fact that you can always put in the full filepath, but the project is being edited by multiple people, so we need the sounds to be located in the solution folder (relative URL).
SO my question is: what is the correct spelling for relative URL? Or even better, is there a simpler method to play 2 sounds simultaneously?
The issue with the "relative URL" you're seeking is that it'd be relative to what? This is a compiled application, not a web page, so neither Uri nor MediaPlayer can assume the answer to that.
That you don't want to hard-code an absolute path doesn't mean you can't construct one at runtime, though. You can use the Assembly.GetExecutingAssembly() method to get the absolute path to your application...
string executableFilePath = Assembly.GetExecutingAssembly().Location;
...and then use the Path class to turn that into an absolute path to your audio file...
string executableDirectoryPath = Path.GetDirectoryName(executableFilePath);
string audioFilePath = Path.Combine(executableDirectoryPath, "BackgroundMusicMix.wav");
...from which you can create an absolute file URL...
Uri audioFileUri = new Uri(audioFilePath);
See Convert file path to a file URI? for some of the peculiarities to consider when constructing a Uri from a filesystem path like that.
By the way, as far as alternatives, I've not used this myself so I don't know how suited it is for what you're doing, but I know NAudio is a thing that exists.
I know this has been asked before, as it's a rookie question, but the other answers on here did not clarify it for me. I an uploading a file then providing a preview. The click event of my upload button is this:
protected void UploadFile (object sender, EventArgs e) {
string folderPath = Server.MapPath ("~/Uploads/");
// If folder does not exist, create it.
if (!Directory.Exists (folderPath))
Directory.CreateDirectory (folderPath);
//Save the File to the Directory (Folder).
FileUpload.SaveAs (folderPath + Path.GetFileName (FileUpload.FileName));
//Display the Picture in Image control.
imgItem.ImageUrl = folderPath + Path.GetFileName (FileUpload.FileName);
}
The upload works fine, but the image control (imgItem) does not display the picture. When I trace it, the URL looks perfect. The URL is:
"C:\\MyStuff\\Source\\Inventory\\Inventory\\UserInterface\\Uploads\\sample.jpg"
That should have worked. What in the world am I doing wrong?
EDIT: I don't feel that this is a good solution at all, but I've found that the program works as expected if I change the last line to this:
imgItem.ImageUrl = "~/Uploads/" + Path.GetFileName (FileUpload.FileName);
Don't anyone have a cleaner, less hardcodey solution?
Well, the
"C:\MyStuff\Source\Inventory\Inventory\UserInterface\Uploads\sample.jpg"
is actually the file path, not the URL. Since you use the word "URL", I assume that you're building a web site. Which means you need an proper URL (e.g. http(s)://yourdomain.com/Source/Inventory/..../sample.jpg)
If you don't how what is the url for your file path, you can use virtual folder in IIS to map it. https://docs.kentico.com/k11/installation/deploying-kentico-to-a-live-server/creating-virtual-directories-and-application-pools-in-iis-7-5-and-7-0
imgItem.ImageUrl = "~/Uploads/" + Path.GetFileName (FileUpload.FileName);
I am trying to modify my track's metadata, but I can't because the file I try to edit is constantly in use. I believe this is because my axwindowsmediaplayer control is still reading from the file. I want it to stop reading from the file so I can edit it, but it seems I can't make its URL property equivalent to nothing- it wants to keep the same URL if I tell it to set the URL to null or " ". If I give it an invalid URL though, it errors. How can I make it so its URL is actually null or better yet, make it so it stops reading from my file altogether?
private void editTrackMetadataToolStripMenuItem_Click(object sender, EventArgs e)
{
Form metaform = new MetaData();
metaform.Show();
Properties.Settings.Default.StopMedia = true;
axWindowsMediaPlayer1.URL = null;//ahahahahahahaha
}
According to the documentation, it is possible to release the current resource with the close method.
Try
axWindowsMediaPlayer1.currentPlaylist.clear();
For folks who are still looking for answers:
close method does not work, it stops the player but you can still click the
play button to play the old one.
axWindowsMediaPlayer1.currentPlaylist.clear(); works as a charm.
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++.
I am using the C# code below to get the url to an xml file. The current page is News.aspx and the XML file is in the same folder, which is why this works fine.
xUrl = Request.Url.GetLeftPart(UriPartial.Path).Replace("News.aspx", "news.xml");
But it feels a little wrong to me, what if News.aspx changed? Is this the right way to do this sort of thing? Or is there a better way to get the URL of a file?
Thanks
I would use Server.MapPath to get the URL of a file.
private string GetPathOfMyXMLFile(string name){
return Server.MapPath("~/Resources/"+name+".xml");
}
you can then get this in your code
// Bla bla load file
string path = GetPathOfMyXMLFile("News");
You could add www.donetnukelabs' suggested answer and pop the name of your xml file into a settings store (web config perhaps), if it's likely to change.
There are many ways you can resolve this, you can introduce constant in the system, or you can use appSettings in web.config to store relative path to the folder for news.xml.
You are right, your current method is not considered good practice.