Access a SAMBA share via C#

A search for "SAMBA" in this forum returned no results, so I am assuming that this question is not already covered somewhere else...

I am trying to programmatically access a Samba share from a C# application. Is there any way to facilitate this using C#/.NET Or is the only option here to write a Win32 Shell app or dll

Any guidance is appreciated...

Thanks,

Outsideshot



Answer this question

Access a SAMBA share via C#

  • Tim erin

    Is there any way to work with network share only with 100% managed code

  • MessDev

    Samba emulates the Microsoft CIFS and SMB protocols.

    So, you should be able to use it just like a regular Microsoft
    Windows network share.

    Use a path like \\sambaserver\fileshare\filename.txt


  • ashish tiwari

    We have a similar situation here, where certain network shares are on different domains that don't have trusts setup between them. If those trusts were there, you should be able to used UNC paths, but without them, you need to authenticate differently to the network share. What you need to do is "map" the drive. Impersonate your Windows user and run the following. It uses WMI to launch the process to map the network share.

    using System.Management;

    ManagementClass processClass = new ManagementClass("Win32_Process");
    object[] methodArgs = { "net use \\\\path\\share /user:domain\\username password", null, null, 0 };
    object result = processClass.InvokeMethod("Create", methodArgs);

    Console.WriteLine("Creation of process returned: " + result);
    Console.WriteLine("Process id: " + methodArgs[3]);


  • CSharpShooter

    I ran the above code using Management namespace on windows 2000 and trying to list directories.

    I am getting System.IO.IOException: Logon failure: unknown user name or bad password.

    User name is local to remote computer.

    Any ideas would be appreciated!

    Thanks!


  • Bill Barnett - MS

    Unfortunately, that is precisely what is not working. The application is running as a service, and when it attempts to access the Samba share using a the path, as you suggest, it fails with the response "Access to path \\SAMBASERVER\FILESHARE\FILENAME.EXT is denied".

    Since Samba share access requires both a windows and a linux auth (outside of the scope of the service, which is by definition not run as a windows user), I need to find a way to simultaneously, programmatically authenticate both as a windows user and a linux user.

    Does this make sense Any other suggestions


  • BretUpdegraff

    I need to enumerate shared resources on remote computer, but without using WMI.

  • Tom Johnson

    Folks,

    I tested this code on win xp and it's working fine.

    But, this code doesn't work on windows 2000.

    Any Ideas

    Thanks!


  • Ben Ronco - MSFT

    Sure. I use the below all the time. Cheers:

    using System;
    using System.Management;
    using System.IO;

    static void Main(string[] args)
    {
    MapDriveECS();
    UploadStuff();
    }//end main

    static void UploadStuff()
    {
    using (StreamWriter sw = new StreamWriter("\\\\lsib\\ecs$\\Josh\\TestFile.txt"))
    {
    // Add some text to the file.
    sw.WriteLine("I am writing in the file");
    sw.WriteLine("-------------------");
    sw.WriteLine("The date is: " + DateTime.Now);
    }
    }//end UploadStuff()

    static void MapDriveECS()
    {
    String myUser = "mydomain\\mydomainuser";
    String myPass = "mydomainuserpassword";
    String cmdString = "net use \\\\lsib\\ecs$ /user:" + myUser + " " +myPass;
    Console.WriteLine(cmdString);
    ManagementClass processClass = new ManagementClass("Win32_Process");
    object[] methodArgs = { cmdString, null, null, 0 };
    object result = processClass.InvokeMethod("Create", methodArgs);

    Console.WriteLine("Creation of process returned: " + result);
    Console.WriteLine("Process ID: {0}", methodArgs[3]);
    }//end MapDrive method


  • DRAYKKO

    Thanks - unfortunately, this is the part of the problem we have solved. We are able to successfully authenticate to the share, we just can't write to it.

    That is, using code pretty similar to the sample you gave above, we establish the map with "net use" and get a seemingly positive non-response (i.e. no "path not found" message). But, when we attempt to write to the share, we get a negative response in the from of "access denied."

    Can you provide sample code for writing to or reading from the samba share

    Thanks again for you assistance - it is much appreciated.


  • Jimmy Xiong

    Normally you only need to set allow the current user's credentials and it should work. Make sure you can browse the folder with Explorer.


  • FannwongCindy

    You have to make sure to set the user promission on both sides, within you Windows system and your SAMBA system!


  • GalenC

    vAlex wrote:
    Is there any way to work with network share only with 100% managed code


    I only see managed code examples here in this thread


  • Access a SAMBA share via C#