Click here to Skip to main content
15,918,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently coding an FileExplorer but I encountered a small problem.

I wanted to set the start location to C:\Users\"UserName" so that the user directly gets his main directorys...

Problem here is, I can't get the code to work, because everything i do, gives out an compilererror: "Cannot implicity convert type string to System.Uri"

Heres my code, I hope someone can help me

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
          string userName = Environment.UserName;
          //Uri NewLocation = null;
          //string NewLocation = @"C:\Users\(userName)";
          webBrowser.Url = userName;
          //Uri openUri = new Uri (userName);
          //System.Uri uri = new System.Uri(userName);
          //webBrowser.Url OpenUrl = new webBrowser.Url "C:\Users\(userName)";

What I have tried:

everything you cann see outcommented in the code
Updated 14-Nov-22 6:48am
Richard Deeming 15-Nov-22 7:50am    
NB: Rather than hard-coding the path, you need to use Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) to get the path of the user's profile, since it is possible to move the profile folder to a different location entirely. Similarly for any other special folders[^], which you can't assume will be within the user's profile folder, or have a specific name.
Richard Deeming 15-Nov-22 7:51am    
And your code as written would cause an infinite loop. Every time the web browser finishes loading, you tell it to navigate again.,

Why are you using a WebBrowser to code a file explorer?

The URL property of a WebBrowser control is a Uri class instance, not a string: WebBrowser.Url Property (System.Windows.Forms) | Microsoft Learn[^]
You can construct a Uri using a string:
Uri uri = new Uri(myString);
But quite what it will fo with a "raw" folder path, I'm not sure - probably complain because it isn't supposed to open them.

I think you need to work out exactly what you expect this to do, and then think about how to go about it - because I don't think a WebBrowser control is the right tool for the job, whatever that job is!
Share this answer
Member 15758581 14-Nov-22 4:17am    
I want to upgrade the Explorer into a BackupTool after i get it working.
What would you recommend as an alternative for the WebBrowser?
Maybe the TreeView ?
OriginalGriff 14-Nov-22 4:49am    
I have no idea why you are using a WebBrowser or what exactly you expect to do with it: it displays HTML data, not folders / file lists, so it's probably not a sensible choice. And because I have no idea what you want to put on display or how you want your use to see it, I can't suggest anything!

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
For me it just works and with a hard coded directory path I get an exact clone of the RH panel of File Explorer. However I can't understand your compiler error. I'd suggest that you clean the solution, delete the content of that method and type it in again. Whatever is wrong must be something very simple but unseen.

private void OpenFolder_Click(Object sender, EventArgs e) {
  String folder = new Uri(@"D:\Radio");
  webBrowser.Url = folder;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900