Using .Net Core 3.1 in VS2019.
The app that will be running on a Linux system, so I can't use any WinAPI stuff.
I'm trying to read a file on a local network system.
The IP and path are correct (using SSH, I can connect to the machine, change to the desired folder, and actually load the desired file into a text editor).
I'm getting the following exception:
Message = "The network name cannot be found. : '\\\\192.168.1.10\\sys\\class\\thermal\\thermal_zone0\\temp'"
The stack trace looks like this:
at System.Net.FileWebRequest.CreateResponse()\r\n
at System.Net.FileWebRequest.<>c.<BeginGetResponse>b__59_0(Object s)\r\n
at System.Threading.Tasks.Task`1.InnerInvoke()\r\n
at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)\r\n
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\r\n
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n
at System.Threading.Tasks.TaskToApm.End[TResult](IAsyncResult asyncResult)\r\n
at System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n
at System.Net.FileWebRequest.GetResponse()\r\n
at EyeSpyPiCLI.DomainReader.GetValue[T](PiSlice slice, T defaultValue) in G:\\dev\\EyeSpyPi\\EyeSpyPiCLI\\Program.cs:line 60\r\n
at EyeSpyPiCLI.Program.Main(String[] args) in G:\\dev\\EyeSpyPi\\EyeSpyPiCLI\\Program.cs:line 16
// inner exception #1
at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean useAsync)\r\n
at System.Net.FileWebRequest.CreateResponse()
// inner exception #2
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)\r\n
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)\r\n
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)\r\n
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync)\r
at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean useAsync)
It looks to me like it is correctly authenticating the credentials, but it can't open the file. I looked at the file using ssh and here's what I get:
-r--r--r-- 1 root root 4096 Jul 5 06:04 temp
I should be able to read the file, right? Or is it because it's in the sys folder? On the other hand, if I provide the appropriate credentials, it should let me read the file, right?
Since this code will eventually be running on a linux box, I can't use Windows-specific libraries. Since the folders in question are essentially system folders (
/etc
,
/sys
and
/proc
), I don't want to share them.
What I have tried:
Many variations of the following (the properties are defined, but those definitions are omitted for brevity):
this.HostUri = new Uri(string.Format("\\\\{0}", this.MachineName));
this.Creds = new NetworkCredential(this.UserID, this.Password);
this.CredCache = new CredentialCache();
this.CredCache.Add(HostUri, "Digest", this.Creds);
...
string path = Path.Combine(this.HostUri.AbsoluteUri, "sys/class/thermal/thermal_zone0/temp");
FileWebRequest request = (FileWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Http.Get;
request.Credentials = this.CredCache.GetCredential(this.HostUri, "Digest");
System.Security.Principal.TokenImpersonationLevel.Impersonation;
using (FileWebResponse response = (FileWebResponse)request.GetResponse())
{
using(Stream stream = response.GetResponseStream())
{
}
}