Append a text file on shared network folder on a computer with Android software


  • I am very very new at this. Trying to append a text file on my computer on a shared folder with an Android software. My code is here:

                button.Click += delegate
                    using( StreamWriter sw = new StreamWriter( @"\\MEHMET-PC\Deneme\deneme1.txt" , true) )
                        sw.WriteLine( "merhaba" );

    I get this error :
    System.UnauthorizedAccessException: Access to the path "/\\MEHMET-PC\Deneme\deneme1.txt" is denied.

    Things I tryed:
     - I tryed to do it on sd card. It worked, i just changed the url to a random folder in my sd card. So I think StreamWriter works.
     - I tryed \\\Deneme\deneme1.txt and other variations like double backslash etc. Every url that works on my explorer. Got the same error.
     - I can just paste this url to my explorer or any other computer in the network, the file and folder is shared.
     - I gave permission to "Everyone" and "NETWORK SERVICE" with "Full Control" in both sharing and security properties of the folder

    I am using Visual Studio 2015. I am debugging it on my phone HTC Desire HD A9191 with Android 2.3 API 10. My pc have Windows 10 and connected to the network via ethernet.

    I am suspecting an Android software permission is missing, because it gave me the same error while trying to save the text file on sd card until I selected permission WRITE_EXTERNAL_STORAGE, but couldn't find the permision related to this issue. Any help would be appreciated.

    Monday, April 17, 2017 1:32 PM


  • The issue isn't with your code but with the fact that the account you're trying to run this code under does not have sufficient privileges to open the given UNC path. Unfortunately this isn't something that you can simply code around. You'll have to resolve the security issues. Given that you're connecting over the phone I suspect you aren't actually running it under a Windows account (since it is Android) that has sufficient privileges.

    The easiest solution, if possible, is to map the drive beforehand and then use the mapped drive. This eliminates the security issues altogether.

    Option 2 is to open the file using the correct credentials. Most apps that need this kind of behavior prompt the user for the UN/PWD and then store that information encrypted. When they connect to the remote UNC they use the provided credentials. Unfortunately .NET does not expose any of the file APIs that accept the credentials as input. So you are limited in what you do. If possible you could use P/Invoke to make the underlying network call to open the file using credentials. There is an article on how to do that here.

    Option 3 is to impersonate the account that does have rights (again, need the UN/PWD). .NET does support impersonation and then you can use the file operations normally.

    Option 4 is to use a third party library that allows UNC connections with network credentials. You'll need to google for those.

    Michael Taylor

    Monday, April 17, 2017 2:09 PM