webclient Exception has been thrown by the target of an invocation [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to use the DownloadFile function of webclient but I used breakpoint to see which line O got error. It just go to catch block when DownloadFile.
try
{
string myStringWebSource = this.curFtpIp + FtpCuPath + "/" + FtpdfName + "/" + this.lbl.Text;
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(this.FtpUserID, this.FtpPassword);
client.DownloadFile(myStringWebSource, SaveFile);
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally { }

I have solved my own question. I found that I set the wrong second parameter. I just use "SaveFile" which is save path but it should be a file name. So, I just modified the SaveFile and it can work.
WebClient.DownloadFile MSDN

Related

C# CodeDom Cant type in " [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to compile a code with codedom which should connect to my ftp server.
But I cant type in the credentials because of the ""...
Look here :
Temp.AppendLine(#"request.Credentials = new NetworkCredential("userid","userpassword");");
If I type " in the code, it automatic ends the content of the brackets...
Help?
You may need to escape the content by using double quotes, like this:
Temp.AppendLine(#"request.Credentials = new NetworkCredential(""userid"",""userpassword"");");
Temp.AppendLine(#"request.Credentials = new NetworkCredential(""userid"",""userpassword"");");
Escape the " with ""

Access denied when writing to a local path? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
After downloading from my FTP Server, and trying to overwrite that file with an updated one, I get an exception saying: "UnauthorizedAccessException was handled", that, Access to the Path 'C:\My Program\My Program\bin\Debug\App_Data' is denied.
This is what my code looks like:
private void downloadFile () {
WebClient wc = new WebClient();
wc.Proxy = null;
wc.Credentials = new NetworkCredential("user", "pass");
byte[] fileData = wc.DownloadData("ftp://user:pass#mysite.tk/updates/App_Data/log.txt");
File.WriteAllBytes(Application.StartupPath + "\\App_Data", fileData);
}
Am I just missing something to set into the WebClient instance that can allow the 'File.WriteAllBytes' to write the file I'm downloading from my FTP Server to my local machine?
If you think about it, this obviously has nothing to do with WebClient. If you took the same sequence of bytes that you got from WebClient, and tried to write it to the same file, you'd get the same result.
In fact, you'd probably get the same result if you tried to write a single byte, and maybe zero bytes.
Like the exception says,
Access to the Path 'C:\My Program\My Program\bin\Debug\App_Data' is denied
Does the App_Data folder even exist?

How can I get from a string of a directory and file name only the file name? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
The code:
'Files' is a List<string> and _indx is an int.
label22.Text = files[_indx];
For example in 'files' in index[0] I have this string:
D:\New folder (45)\converted.avi_Automatic\Lightning 0 Length 2 [91 - 93]\000091.bmp
But instead in label22.Text I want it to show me only '000091.bmp' without the rest of the directory path.
How can I do it ?
Use Path.GetFileName:
label22.Text = Path.GetFileName(files[_indx]);
I believe you are looking for Path.GetFileName():
label22.Text = Path.GetFileName(files[_indx]);
Path.GetFileName(fileName) returns the file name without the directory.
taken from http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.100).aspx
The simplest way is
Path.GetFileName(files[_indx]);

Why am I getting a FileNotFoundException when the file is there? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to save a file to the server and then load into a reader for it to be downloaded. However, I am getting a FileNotFoundExeption. I save to the exact same path, manually open the directory and can see the file there. However, reading it results in the exception. This is my first time trying his - am I doing something wrong?
try
{
using (StreamReader reader = new
StreamReader(HttpContext.Current.Server.MapPath(#"~/Downloads/data.text")))
{
// do something
}
}
catch (Exception)
{
}
Double-check the file name! In one of your comments you used the file name data.txt and not the name data.text. I suppose it's just a typo in your code.

Label null exception [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am receiving a null exception every time I try to send the HttpContext.Current.User.Identity.Name to the label. Any ideas why?
This is what im using.
String Utilizador = Page.User.Identity.Name;
Response.Write(Utilizador);
Label1.Text = Utilizador;
Since you are authenticated and you're getting the NullReferenceException in Label1.Text = Utilizador;, you don't have a reference to that label.
everything is in master page for now
Rename it on the MasterPage, compile it, name it again Label1(i would strongly recommended to use better names for example LblUserName). Then recompile it.
You need to disable non-authenticated users. In web.config:
<authorization><deny users="?" /></authorization>
You need to instantiate your Label
Label Label1 = new Label();
Label1.Text = Utilizador;
Could you try to add authentication tag in web.config as below.
<authentication mode="Windows" />

Categories