[Mono-list] Mono C# and Windows Shares

Francisco T. Martinez martinf@mfconsulting.com
Sat, 22 Jan 2005 04:54:24 -0600


Trygve Falch wrote:

>On Fri, 2005-01-21 at 10:55 +0000, Dick Porter wrote:
>  
>
>>On Fri, 2005-01-21 at 11:27 +0100, Trygve Falch wrote:
>>    
>>
>
>  
>
>>>File.Copy using UNC-paths under windows works OK, but not surprisingly
>>>under say Linux.
>>>      
>>>
>>We don't support UNC paths.  To do so would I think involve building
>>samba into mono, which is not sane.
>>    
>>
>
>Yeah, I know. But my question was rather; 'Is there a way to do that
>(eg. copying files to a windows-share from Linux using Mono) any other
>way?'
>
>  
>
Dicks answer is pretty much definitive.  However, in the event that what 
I will explain below can help you...

If you were to mount the samba share into the file system of the Linux 
machine running Mono, then you could address the Windows shares as if 
they were a local path.  Of course, at that point it is the operating 
system facilities that make those files available to Mono apps and not 
any of the Mono framework class libraries.

Once samba client is installed on your system you could do something 
like the example below.

In this example you have su as root so that you can mount volumens and 
you create a direcotry in your file system called smbvol so it can serve 
as the mounting point.
Example 1:

mount -t smbfs -o "username=jmartinez,password=monito" 
//mfc13bdc/MyShare /mnt/smbvol

using System;
using System.IO;

namespace Mfcon.General.Test
{

    public class DirList
    {
        public static void Main(string []args)
        {
            string []files;
           
            files = Directory.GetFiles("/mnt/smbvol");

            foreach(string fileName in files)
            {
                Console.WriteLine(fileName);
            }
        }
    }
}

Example 2:

using System;
using System.Diagnostics;

namespace Mfcon.General.Test
{

    public class DirList
    {
        public static void Main(string []args)
        {
            string dirOutPut;

            dirOutPut = DirList.ListFilesInSmbVol("//mfc13bdc/MyShare", 
"MFCONSAULT/jmartinez", "monito");
           
            Console.WriteLine(dirOutPut);
        }

        public static string ListFilesInSmbVol(string service, string 
userName, string userPassword)
        {
            string retVal;

            ProcessStartInfo pi = new ProcessStartInfo ();
            pi.FileName = "smbclient";
            pi.RedirectStandardOutput = true;
            pi.UseShellExecute = false;
            pi.Arguments = String.Format("{0} {1} -U {2} -c \"dir\"",
                service, userPassword, userName); 
           
            Process p = null;
            try
            {
                p = Process.Start (pi);
            }
            catch (Exception e)
            {
                Console.WriteLine("Couldn't run smbclient: " + e.Message);
                return null;
            }
          
            retVal = p.StandardOutput.ReadToEnd ();       
            p.WaitForExit ();
            if (p.ExitCode != 0)
            {
                Console.WriteLine("Error running smbclient.");
                return null;
            }

            if (retVal != null)
            {
                p.Close ();
                return retVal;
            }

            p.Close ();

            return null;
        }
    }
}


Hope this helps