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.
Related
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.
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
I've written a simple code. I don't understand, what happens here. Please explain to me, where will I find a given upload file
WebClient client = new WebClient();
string uri = "http://localhost:8080/sample.txt";
client.Credentials = CredentialCache.DefaultCredentials;
string Filename = "F:\sample_test.txt/docx;
byte[] arrReturn = client.UploadFile(uri, "POST", Filename);
Well, to start with this clearly isn't your real code, as this line won't compile:
string Filename = "F:\sample_test.txt/docx/";
\s isn't a valid escape sequence However, assuming you had working code, what will happen is that your application would make an HTTP POST request with the contents of the file, to the given URI - in this case http://localhost:8080/sample.txt. It's entirely up to the server what it does with the request.
It could save the file on disk somewhere
It could save it to a database
It could post it to another web service
It could completely ignore the contents, and not save it anywhere
Nothing is guaranteed by the act of uploading the file - you're just making a request with some data.
Now if your URI really involves localhost, then it's uploading the file to the computer you're on - so you should be in control of what the web server listening on port 8080 is going to do with upload requests. Again, we can't tell you what it will do - it's up to the server.
It might be your filename:
string Filename = "F:\sample_test.txt/docx/";
Change the forward slashes to blackslashes, and give an actual filename as well, not just the path:
string Filename = #"F:\sample_test.txt\docx\";
Or
string Filename = "F:\\sample_test.txt\\docx\\";
Actually, the filename as is doesn't make much sense - I'm not sure how UploadFile would process it, even with the slashes going the correct way, as you appear to have a filename followed by a directory...? Shouldn't it actually be:
string Filename = #"F:\docx\sample_test.txt"?
It could be the URI - you're specifying the filename as well as the URI.
Did you look at the location specified in the URI? http://localhost:8080/?
If it's not there, then try wrapping your code in a try-catch block to see if any exceptions are thrown:
try
{
byte[] arrReturn = client.UploadFile(uri, "POST", Filename);
}
catch (Exception ex)
{
// do something here - in the debugger, you can inspect ex.Message to see the exception
}
I am trying to move a file from server \\abc\\C$\\temp\\coll.zip to another server
\\def\\c$\\temp.
I am trying to use File.Copy(source,destination).
But I am getting the error in source path saying: Couldn't find the part of the path.
I am not sure what is wrong with the source path.
You could use a C# # Verbatim and also use checks in the code like this:
string source = #"\\abc\C$\temp\coll.zip";
string destination = #"\\def\c$\temp\coll.zip";
string destDirectory = Path.GetDirectoryName(destination)
if (File.Exists(source) && Directory.Exists(destDirectory)) {
File.Copy(source, destination);
}
else {
// Throw error or alert
}
Make sure that your "\" characters are escaped if you are using C#. You have to double the backslashes or prefix the string literal with #, like this:
string fileName = #"\\abc\C$\temp\coll.zip";
or
string fileName = "\\\\abc\\C$\\temp\\coll.zip";
looks like you need two backslashes at the beginning:
\\abc\C$\temp\coll.zip
\\def\c$\temp
Make sure you are using a valid UNC Path. UNC paths should start with \ not just . You should also consider using System.IO.File.Exists(filename); before attempting the copy so you can avoid the exception altogether and so your app can handle the missing file gracefully.
Hope this helps
It could be the string you are using for the path. If it is exactly as you have entered here I believe you need double backslashes. "\\" before the server name.
I always use network shares for that kind of work, but UNC path's should be available too.
Don't forget that you need to escape your string when you use \'s. Also, UNC paths most of the time start with a double .
Example:
\\MyComputerName\C$\temp\temp.zip
Actually I missed # before the two strings.The source and the destination path.
That is why it was giving error.
In a WinApp I am simply trying to get the absolute path from a Uri object:
Uri myUri = new Uri(myPath); //myPath is a string
//somewhere else in the code
string path = myUri.AbsolutePath;
This works fine if no spaces in my original path. If spaces are in there the string gets mangled; for example 'Documents and settings' becomes 'Documents%20and%20Setting' etc.
Any help would be appreciated!
EDIT:
LocalPath instead of AbsolutePath did the trick!
This is the way it's supposed to be. That's called URL encoding. It applies because spaces are not allowed in URLs.
If you want the path back with spaces included, you must call something like:
string path = Server.URLDecode(myUri.AbsolutePath);
You shouldn't be required to import anything to use this in a web application. If you get an error, try importing System.Web.HttpServerUtility. Or, you can call it like so:
string path = HttpContext.Current.Server.URLDecode(myUri.AbsolutePath);
It's encoding it as it should, you could probably UrlDecode it to get it back with spaces, but it's not "mangled" it's just correctly encoded.
I'm not sure what you're writing, but to convert it back in asp.net it's Server.UrlDecode(path). You also might be able to use LocalPath, rather than AbsolutePath, if it's a Windows app.
Just use uri.LocalPath instead
Uri also has a couple of static methods - EscapeDataString and EscapeUriString.
Uri.EscapeDataString(uri.AbsolutePath) also works
Use HttpUtility:
HttpUtility.UrlDecode(uri.AbsolutePath)