Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):

C#
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())
    {
        // handling the returned data (I haven't been able to get this far yet)
    }
}
Posted
Updated 7-Jul-20 0:27am
v3
Comments
George Swan 5-Jul-20 14:51pm    
Have you installed Samba on your box? I use smb://boxname.local/pathToShare/ the 'local' tag is important
#realJSOP 6-Jul-20 13:30pm    
Samba is on the box I work on, but not all of the remote machines. BTW, I haven't tried ".local" yet.
Richard Deeming 6-Jul-20 9:34am    
NB: Setting FileWebRequest.Credentials[^] doesn't currently have any effect.

The bit that nags me is
FileWebRequest request = (FileWebRequest)WebRequest.Create(path);
if you're going through a Web-Server, then, the filesystem you can 'see' is relative to the Web-Server isnt it ?

That doesnt mean you can't add a link under the Web-Server's public file space to your sys/..../file
 
Share this answer
 
Comments
#realJSOP 5-Jul-20 11:29am    
Well, I don't want to have to do something on the remote device (happens to be a raspberry pi) to make it work, but at the same time, this code will eventually be runnign from a .net core web app on a linux box, so I can't use windows-specific code to do it. I don 't know any other way to do it. :/
Doesn't a file request path Uri have to start with "file://serverName/path"??

I haven't tried this. It's just a wild ass guess.
 
Share this answer
 
v2
Comments
#realJSOP 5-Jul-20 13:03pm    
yeah, that's what the path looks like. No workee. The exception is showing it that way, but i'm using a Uri and sending it as "file://...".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900