I've read a few posts around this, but I have a question that can't hasn't been explicitly answered, so I'm posting this.
I am creating an C# Android app using Mono that has the option to backup files to multiple online shares, OneDrive, Google Cloud, FTP and local network share.
All the other methods are sorted, except uploading to local network share. The idea is that people will be using this when they are in a high speed local wireless network and this will copy data very quickly directly onto a share on a server. \SERVER\share
The problem is that, you can't pass credentials to File.Copy() So what's the best method to go down?
1.) It's been said that you can P\Invoke WNetAddConnection2 from MPR.dll. This works great on Windows, but gives DllNotFound on Android. Is it possible to copy this library to the phone and P\Invoke it for this one function? How to provide user name and password when connecting to a network share
2.) It is possible to use Impersonation using WindowsIdentity, again, works great on a windows machine, but does it work on an Android phone?
3.) It's possible to scan\access a local network using SMB protocol as I understand, I've been looking at the CIFSClient library, but it's not seeming to work for me at the minute.
4.) Is there a Java method that can be called from Mono that can simply do a copy over a network?
Cheers for any help, c
EDIT : All sorted now. The JCIFS binding library did the trick.
1.) Is it possible to copy this (MPR.dll) library to the phone and P\Invoke it for this one function?
No (that is a native Win32 lib)
2.) It is possible to use Impersonation using WindowsIdentity, again, works great on a windows machine, but does it work on an Android phone?
No
3.) It's possible to scan\access a local network using SMB protocol as I understand, I've been looking at the CIFSClient library, but it's not seeming to work for me at the minute.
Yes (in regards to SMB)
? (in regards to CIFSClient, never used it)
4.) Is there a Java method that can be called from Mono that can simply do a copy over a network?
No (in relation to a SMB share)
Android and SMB:
I have used jCIFS which is a pure Java library that implements the SMB protocol on Xamarin Android projects.
Disclaimer: This is a jCIFS Java to C# binding library from my public repo:
Example Usage of Jcifs.Smb.SmbFileInputStream:
// This is NOT best-practice code, just showing a demo of an Jcifs api call
public async Task getFileContents ()
{
await Task.Run (() => {
var smbStream = new SmbFileInputStream ("smb://[email protected]/code/test.txt");
byte[] b = new byte[8192];
int n;
while ((n = smbStream.Read (b)) > 0) {
Console.Write (Encoding.UTF8.GetString (b).ToCharArray (), 0, n);
}
Button button = FindViewById<Button> (Resource.Id.myButton);
RunOnUiThread(() => {
button.Text = Encoding.UTF8.GetString (b);
});
}
).ContinueWith ((Task arg) => {
Console.WriteLine (arg.Status);
if (arg.Status == TaskStatus.Faulted)
Console.WriteLine (arg.Exception);
}
);
}