C# WebBrowser Navigate to File Path fails (file exists) - c#

I have a web browser in C# that I want to make navigate to a path (html file) on my local pc.
I tried using this:
if (File.Exists(Path + b.HTML))
{
browserCom1.Navigate(Path + b.HTML);
}
The file Exists, but the browser is keep opening an error of Internet Explorer: "cannot find file:///(my path here)"
It is weird because the file is correct. for example if I use:
System.Windows.Forms.OpenFileDialog browseFile = new
System.Windows.Forms.OpenFileDialog();
browseFile.ShowDialog();
String path = browseFile.FileName;
browserCom1.Navigate(path);
and I select the same file that it tried navigating to before, it works.
If I print the above brwseFile File Name to Console(which is the same as my Path+b.HTML by the way), and copy-paste it into the Navigate(...) Function (changing each '\' to '//') it Doesn't work.
I have no Idea what to do.
I tried something else like:
String path=(File.Open(Path + b.HTML, FileMode.Open).Name);
browserCom1.Navigate(path);
but the application keep getting freezed upon this.
I also tried with new URI(path) and all.
How can I simpley navigate to a HTML file on my computer?

You have http slashes, but should have file system slashes, like c:\something\something.html

I had the same problem. Resolved when I cleaned for double \\ in the code.
If that's not your problem - your problem may be some else problem related to parsing from string to uri.
my path was like this: c:\users\someone1\\myFolder\protocol.htm

Related

Using Process.Start to open explorer

Edit: solution found, will note under image at the end of the question
After a bunch of research here on SO, I found that the way to open explorer with a selected file was:
Process.Start("explorer.exe", "/select, " + path);
However when I do this with controlled input, Explorer opens just its main window, however when I harcode the function call to the same value that's in the path variable (In my control test its a text file in C:\Temp) it works. So if I do the above when path is "C:\Temp\test.txt" It does't open explorer in the temp folder, however when I do:
Process.Start("explorer.exe", "/select, C:\\Temp\\test.txt");
It works perfectly, opening explorer and highlighting the file. What is happening here? Is there something wrong with the internal formatting on my string variable or something?
(Additionally, I ran into the same issue using the path variable to open a FileInfo. Hardcoded to the same value would work, but using the variable gave me a "the given path's format is not supported" exception")
Image showing that path and the harcoded value are the same:
The 2 explorer windows (cropped for Security) are the results of the 2 respective calls. The one with the variable shows te basic explorer main page. The one that's hardcoded shows the file selected as expected.
Edit: There was a hidden Left-To-Right Format character hidden in the front of the string.
public static class Program
{
static void Main()
{
Explore("C:\\Users\\art_g\\Desktop\\Sample.txt");
}
static void Explore(string path) =>
Process.Start("explorer.exe", "/select, " + path);
}
Works like a charm. Check your path string.

File.Copy keeps trying to find a server url by looking for it in my C drive

I am trying to run the File.Copy function so that I can use a template file I have to make a new word document that the code will fill out. Whenever the code gets to the file.copy function I get the error
"IOException: The filename, directory name, or volume label syntax is incorrect : 'C:\Software\DRAT\DRAT*serverpath*\SoftwareReleaseTool\Trunk\ReleaseNotesTemplates\ReleaseNotes1.docx'"
I want the filename to be
"serverpath\SoftwareReleaseTool\Trunk\ReleaseNotesTemplates\ReleaseNotes1.docx"
The first part, "C:\Software\DRAT\DRAT\" is the directory that the project is in. I cannot find out why it keeps looking in this directory for the server link.
I watch the local values before the function is run and they are correct. I get
"serverpath/Fusion/Main\Releases\Notes\VCM-1.1.43-SoftwareReleaseNotes.docx"
for notes.ReleaseNotesPath and
"serverpath/SoftwareReleaseTool/Trunk/ReleaseNotesTemplates/ReleaseNotes1.docx"
for templatePath.
If I use the paths to the same files that are on my local drive it works, but I need to use the SVN server links so that my co-workers can access the same file from their computers.
Here is the file.copy function that is called:
File.Copy(templatePath, notes.ReleaseNotesPath, true);
templatePath is filled out by the user, where I input "serverpath/SoftwareReleaseTool/Trunk/ReleaseNotesTemplates/ReleaseNotes1.docx"
notes.ReleaseNotesPath is defined here:
notes.ReleaseNotesPath = buildFiles[0] + #"\Releases\Notes\VCM-" + model.ReleaseVersion + "-SoftwareReleaseNotes.docx";
the buildFiles[0] part of it is "serverpath/FusionTest/Main"
I should be making new word document but instead I keep getting the same IOException every time.
File.Copy only works in your local file system. You should try something like System.Net.WebClient.DownloadFile
e.g:
using (var client = new WebClient())
{
client.DownloadFile("https://ags-iv-engrpub/svn/SoftwareReleaseTool/Trunk/ReleaseNotesTemplates/ReleaseNotes1.docx", "ReleaseNotes1.docx");
}

How to give path to an text file in Solution using XDocument?

I want to load a xml document Swedish.xml which exists in my solution. How can i give path for that file in Xamarin.android
I am using following code:
var text = File.ReadAllText("Languages/Swedish.txt");
Console.WriteLine("text: "+text);
But i am getting Exception message:
Could not find a part of the path "//Languages/swedish.txt".
I even tried following lines:
var text = File.ReadAllText("./Languages/Swedish.txt");
var text = File.ReadAllText("./MyProject/Languages/Swedish.txt");
var text = File.ReadAllText("MyProject/Languages/Swedish.txt");
But none of them worked. Same exception message is appearing. Build Action is also set as Content. Whats wrong with the path? Thanks in advance.
Just try with this
string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Languages", "Swedish.txt");
var text = File.ReadAllText(startupPath);
Try...
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments)+"/Languages/Swedish.txt"
If you mark a file as Content Type, it will be included in the app bundle with the path that you are using within your project file. You can inspect the IPA file (it's just a renamed zip) that is created to verify that this is happening.
var text = File.ReadAllText("Languages/Swedish.txt");
should work. The file path is relative to the root of your application. You need to be sure that you are using the exact same casing in your code that the actual file uses. In the simulator the casing will not matter, but on the device the file system is case sensitive, and mismatched casing will break the app.
I've looked into this before and never found any solution to access files in this way. All roads seem to indicate building them as "content" is a dead end. You can however place them in your "Assets" folder and use them this way. To do so switch the "Content" to "AndroidAsset".
After you have done this you can now access the file within your app by calling it via
var filename = "Sweedish.txt";
string data;
using
(var sr = new StreamReader(Context.Assets.Open(code)))
data = sr.ReadToEnd();
....

User Permissions issue

I am using Visual Studio C# to parse an XML document for a file location from a local search tool I am using. Specifically I am using c# to query if the user has access to certain files and hide those to which it does not have access. I seem to have files that should return access is true however because not all files are local (IE some are web files without proper names) it is not showing access to files it should be showing access to. The error right now is caused by a url using .aspx?i=573, is there a work around or am I going to have to just remove all of these files... =/
Edit: More info...
I am using right now....
foreach (XmlNode xn in nodeList)
{
string url = xn.InnerText;
//Label1.Text = url;
try
{ using (FileStream fs = File.OpenRead(url)) { }
}
catch { i++; Label2.Text = i.ToString(); Label1.Text = url; }
}
The issue is, when it attempts to open files like the ....aspx?i=573 it puts them in the catch stack. If I attempt to open the file however the file opens just fine. (IE I have read access but because of either the file type or the append of the '?=' in the file name it tosses it into the unreadable stack.
I want everything that is readable either via url or local access to display else it will catch the error files for me.
I'm not sure exactly what you are trying to do, but if you only want the path of a URI, you can easily drop the query string portion like this:
Uri baseUri = new Uri("http://www.domain.com/");
Uri myUri = new Uri(baseUri, "home/default.aspx?i=573");
Console.WriteLine(myUri.AbsolutePath); // ie "home/default.aspx"
You cannot have ? in file names in Windows, but they are valid in URIs (that is why IE can open it, but Windows cannot).
Alternatively, you could just replace the '?' with some other character if you are converting a URL to a filename.
In fact thinking about it now, you could just check to see if your "document" was a URI or not, and if it isn't then try to open the file on the file system. Sounds like you are trying to open any and everything that is supplied, but it wouldn't hurt to performs some checks on the data.
private static bool IsLocalPath(string p)
{
return new Uri(p).IsFile;
}
This is from Check if the path input is URL or Local File it looks like exactly what you are looking for.
FileStream reads and writes local files. "?" is not valid character for local file name.
It looks like you want to open local and remote files. If it is what you are trying to do you should use approapriate metod of downloading for each type - i.e. for HTTP you WebRequest or related classes.
Note: it would be much easier to answer if you'd say: when url is "..." File.OpenRead(url) failes with exception, mesasge "...".

WebBrowser control in windows form application using C#

I am trying to create a form with webbrowser control. I am trying to open a local html file in the webbrowser control. I have the html file in Help_Print folder, so I am using the code below to specify the Url of the webbrowser.
wbPrint.Url = new Uri("file:///" + Application.StartupPath + "\\Help_Print\\help.html");
When the form shows the webbrowser control has an error "This program cannot display the webpage". I have checked the file and folder location.
But when I try this:
wbPrint.Url = new Uri("file:///" + Application.StartupPath + "/help.html");
after copying the html file to the application startup location it works properly.
Can anyone please explain why is this happening, as I want to keep all html file in a separate folder.
Don't use black-slashes in file urls. See the bizarre and unhappy story of file urls.
I found a solution to my problem even-though I am not sure why my previous code was not working. I changed two thing:
(i) I remove Uri, as I was suggested in answer to a different question of mine, that for local html files I am not required to use Uri. So here is the code that work.
String sitePath = null;
try
{
sitePath = Application.StartupPath + #"\Print_Help\help.html";
wbHelp.Navigate(sitePath);
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString() + "\nSite Path: " + sitePath);
return false;
}
return true;
(ii) The other thing I did was to create the Print_Help folder manually. Previously it was created when I Build my Project as the html file was marked the property Copy to Output Directory as Copy Always.
I think the second change has more to do with my problem solution than the first. Please comment if you understand the logic.
use syntax: file://c:/xyz.html

Categories