I want to have access to a location in my laptop using C#, the explicit destination is
"\\\\file\\doc/"+ GetImageNameFromPLC()+".jpg"
but I can not get this with C#, this is how it looks currently
Uri serverUri = new Uri(@"\\file\doc/"+ GetImageNameFromPLC()+".jpg");
WebClient request = new WebClient();
request.Credentials = new NetworkCredential("", "");
try {
byte[] newFileData = request.DownloadData(serverUri.ToString());
return newFileData;
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return new byte[0];
}
private string GetImageNameFromPLC()
{
In the first line I get the error: An object reference is required for the non-static field, method, or property.
Could you please advice how the string "\\file\\doc/"+ GetImageNameFromPLC()+".jpg" should be written? Thank you in advance for your responses.
Actually the problem is in the slash /
in Windows UNC paths use \\
instead. And also you should not use WebClient to download files from network share. You can directly read text content from the server using System.IO.File.ReadAllText, and also we need to get the code of the function GetImageNameFromPLC()
errors may also reside there. You should use private static
instead.
The method where the error occurs is likely static
(we can't see that in the code given). You have two options now:
GetImageNameFromPLC()
static, toovar obj = new ...()
) and then call obj.GetImageNameFromPLC()
.Please note, that your code will likely still not work, even if it compiles.
/
instead of \\
in the path. Also check the return value of GetImageNameFromPLC()
- if it returns /
as well, you need to replace it.WebClient
will try to access stuff via HTTP protocol, but a network share uses SMB protocol instead. You can access network files like regular files on a local hard disk.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.