I have a HTML file in C:\Users\myusername\AppData\Roaming\myapp\file.html. I am accessing the file through a web Browser in my C# application to preview it from within the app.
However, when the app is put onto another computer, the address in webBrowser1 is still specific to my username, and therefore other people cannot access the preview.
Is there a way to get to the file as a URL in my web Browser without having the hard coded username in the URL?
What I have tried:
- C:\Users\%USERNAME%\AppData\Roaming\myapp\file.html
- C:\Users\AppData\Roaming\myapp\file.html
Thanks!
Here is the code I used after I was helped:
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string address = Path.Combine(folderPath + @"\myapp\file.html");
webBrowser1.Navigate(address);
If you want to get the name of the current logged in user you have to read
Environment.UserNameproperty.Moreover if you need to access the
AppDatadirectory for the roaming user you can get the folder path without hard-coding anything (do not forget that users directory isn’t alwaysc:\userson every Windows version and path forAppDatamay vary too):In you case simply append the file name:
Notes
If, for any reason, you need to use environment variables then you have first to expand them: