c# webbrowser coding to navigate local to html - c#

I'm trying to put a local html location address to the web browser in my C# application but always failed. I'm using debug mode now so the html files had already copied into my Debug folder because i put copy always in the copy to output option.
Below is my code:
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string filePath = Path.Combine(appPath, "index.html");
webBrowser1.Navigate(new System.Uri(#"file://"+ filePath));
There always this error coming out using that way:
An unhandled exception of type 'System.UriFormatException' occurred in System.dll
Any idea what went wrong?

Its a bit unclear but try this out. Hope this may help.
webBrowser1.Navigate(new Uri(#"your File Name"))
Or
webBrowser1.Navigate(new Uri(AppDomain.CurrentDomain.BaseDirectory == "\\File Name")
Edit:
May be this causing error,
webBrowser1.Navigate(new System.Uri("#" filePath + "file://"));

You just need to change your code slightly - here is the correct syntax:
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string filePath = Path.Combine(appPath, "index.html");
webBrowser1.Navigate(new System.Uri(filePath));
The issue is that CodeBase returns the path in URI format already so you do not need to add the additional the "file://" to the path.

Related

System.IO.IOException file already exists

I'm using the code below for my application to copy to appdata roaming folder.
The problem I'm having is that if application is run once it works well but if i re-run it it gives me this error :
System.IO.IOException: 'The file
'C:\Users\Administrator\AppData\Roaming\app.exe' already exists.'
Can I somehow fix this exception because once this happen my application stop working ?
string exePath = Application.ExecutablePath;
string copyPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
copyPath += "\\\\" + Path.GetFileName(exePath);
File.Copy(exePath, copyPath);
You just to need to specify that overwriting is allowed:
File.Copy(exePath, copyPath, true);

Why I am getting "The given path's format is not supported" error

I am unable to rename a file located in (D drive) using c#. I am getting the error
Exception Details: System.NotSupportedException: The given path's
format is not supported.
every time.
I am using
string oldfilename = #"D:\abc\file.txt";
string newfilename = #"D:\abc\tree.txt";
System.IO.File.Move(oldfilename, newfilename);
but I am getting the error on the last line.
I also tried changing the first 2 lines to
string oldfilename = "D:\\abc\\file.txt";
string newfilename = "D:\\abc\\tree.txt";
I also ensured that the file "file.txt" exists. Tried to use different location.
I also tried Reading contents of the file.txt, but I am getting same error.
I searched all the Questions on SO, But with no luck I could get this issue resolved. I think there is some issue with the ":" that I am using after the drive letter while specifying the path. Please guide me.
I tried this on another computer but still it didn't work!(Surprising).
I have managed to solve this issue by moving "file.txt" to my project folder.
Now I am using
string oldfilename = "file.txt";
string newfilename = "tree.txt";
System.IO.File.Move(oldfilename, newfilename);
And this Works!
This doesn't seems to be an answer to this question(for me), but It has really worked for me.
Why donĀ“t you utilize:
My.Computer.Filesystem.RenameFile("D:\file.txt", "tree.txt")
I try it by myself and these variants worked:
string oldfilename = "C:\\Users\\User\\Downloads\\WorkTemp\\file.txt";
string newfilename = "C:\\Users\\User\\Downloads\\WorkTemp\\file2.txt";
System.IO.File.Move(oldfilename, newfilename);
string oldfilename = #"C:\Users\User\Downloads\WorkTemp\file1.txt";
string newfilename = #"C:\Users\User\Downloads\WorkTemp\file2.txt";
System.IO.File.Move(oldfilename, newfilename);
According to the reference source: http://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs#749
NotSupportedException will be thrown if the index of the : in your path is at the third position or later. (One would expect : to be the second character) Are you sure there are no zero-width combining characters or other similar Unicode shenanigans going on in your source?
from that answer

Error when downloading the file C#

My code:
string dir = "/Users/valeria/Desktop/screening/"+cell;
string remoteUri ="http://www.broadinstitute.org%2Fcmap%2FviewScan.jsp%3Ftype%3DCEL%26scan%3D"+p;
string pFileName = dir + "/p";
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFile(remoteUri, pFileName);
}
My program creates a file pFileName, but doesn't download anything because I get the following exception:
Unhandled Exception: System.Net.WebException: Could not find a part of
the path
"/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA".
---> System.IO.DirectoryNotFoundException: Could not find a part of the path
"/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA"
What's wrong?
The escaped URI certainly isn't helping. URL-encoding is generally only used when the item you are encoding is being appended to a URL; encoding the URL itself is unnecessary and can lead to other problems.
I would strongly suggest changing
string remoteUri="http://www.broadinstitute.org%2Fcmap%2FviewScan.jsp%3Ftype%3DCEL%26scan%3D"+p;
to
string remoteUri ="http://www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan="+p;
and re-trying.
The variable cell is - as pointed out by Adrian Wragg - wrong.
Your error already indicates your problem (the bold part is what's in your cell-variable)
"/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA"
So make sure you provide a valid path.
If you don't believe me, you can check your filepath like this:
If (!System.IO.Directory.Exists(dir))
{
Stop; //<== if it hits here, we are right. ;-)
}
There are two problems with your code:
1: You are using encoded Uri, so you have to decode your Uri using System.Web.HttpUtility:
string decodedUri = HttpUtility.UrlDecode(remoteUri);
Then, you will get proper Uri:
http://www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA
Which you should pass to myWebClient:
myWebClient.DownloadFile(decodedUri, pFileName);
2: Your cell variable points to url, so you have to fix it. You can assign it as string.Empty or remove it temporary to see if that solution works.

How to programmatically convert a .cal file to a .cg4?

In windows explorer I am able to just change the file extension of a .cal file to a .cg4 file. When I do it I get a warning; "changing the file extension might render the file useless, do you still wish to change?" or something like that (OS not in english), but if I click "yes" it works.
But trying to do this programmatically with C# doesn't work. I get an error that states: "The given path's format is not supported."
I'm using File.Move for renaming/converting and that's where the error occurs.
File.Move(directory + fileNameWithoutExtension + ".cal", directory + fileNameWithoutExtension + ".cg4");
What can I do?
does this work?
var pathSource = System.IO.Path.Combine(directory, fileNameWithoutExtension, ".cal");
var pathDest = System.IO.Path.Combine(directory, fileNameWithoutExtension, ".cg4");
File.Move(pathSource, pathDest);
if it also throws an error, check if
fileNameWithoutExtension
already contains the data of
directory

ArgumentException: 'Illegal characters in path' in C#

The error I am receiving upon executing my code is: 'ArgumentException was unhandled. Illegal characters in path.'
I am using the following code to access my .xml file.
string appPath = Path.GetDirectoryName(System.Environment.CommandLine);
FileInfo fi = new FileInfo(appPath + #"\Books.xml");
I am doing this for a console application, as opposed to a WinForm. I have been exhaustively googling, as well as searching SO, for some time.
This is part of a homework project. This is the only problem I am running into however.
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FileInfo fi = new FileInfo(Path.Combine(appPath, "Books.xml"));
System.Environment.CommandLine does not return a path - it returns the value of the command line that was executed to run the application.
You probably need to use Assembly.GetExecutingAssembly().Location (as Furqan Safdar posted in his answer) instead.
The format of the EXE path returned by CommandLine is funky, so you need to do something like this:
string appPath = Path.GetDirectoryName(System.Environment.CommandLine.Trim('"', ' '));
That should work.
Use this code to get application directory:
var rootDirectory = AppDomain.Current.BaseDirectory;
Good luck!

Categories

Resources