Wednesday, May 12, 2010

How to use the NFS Client c# Library

I add a wiki page that explains how to use the NFS Client c# .net library in your project.

NekoDrive uses a Library written in C# on .NET 2.0. that wraps the C++ NFS implementation. In order to use this library in your project download NekoDrive and copy in your project NekoDrive.NFS.dll, NFSv2.dll and NFSv3.dll. Add a reference in your project to NekoDrive.NFS.dll. Don't forget to include NFSv2.dll and NFSv3.dll as a content to deploy.

you download this library here:

http://code.google.com/p/nekodrive/

Example 1 - Connect to NFS server and get the exported devices

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using NekoDrive.NFS;
using NekoDrive.NFS.Wrappers;


namespace Example1
{
class Program
{
static void Main(string[] args)
{
using(NFS nfs = new NFS(NFS.NFSVersion.v2))
{
if (nfs.Connect(IPAddress.Parse("161.55.201.250")) == NFSResult.NFS_SUCCESS)
{
foreach(string device in nfs.GetExportedDevices())
Console.WriteLine(device);
nfs
.Disconnect();
}
}
}
}
}

Example 2 - Connect to NFS server, mount the first exported device and get the file list

namespace Example2
{
class Program
{
static void Main(string[] args)
{
using(NFS nfs = new NFS(NFS.NFSVersion.v2))
{
if (nfs.Connect(IPAddress.Parse("161.55.201.250")) == NFSResult.NFS_SUCCESS)
{
List devices = nfs.GetExportedDevices();
if(devices.Count > 0)
{
if(nfs.MountDevice(devices[0]) == NFSResult.NFS_SUCCESS)
{
foreach(string item in nfs.GetItemList())
{
NFSAttributes attrib = nfs.GetItemAttributes(item);
Console.WriteLine(item + " " + attrib.cdateTime.ToString() + " " + attrib.size);
}
nfs
.UnMountDevice();
}
}
nfs
.Disconnect();
}
}
}
}
}

Example 3 - Connect to NFS server, mount the first exported device and download a file in the root folder (.)

namespace Example3
{
class Program
{
static void Main(string[] args)
{
using(NFS nfs = new NFS(NFS.NFSVersion.v2))
{
if (nfs.Connect(IPAddress.Parse("161.55.201.250")) == NFSResult.NFS_SUCCESS)
{
List devices = nfs.GetExportedDevices();
if(devices.Count > 0)
{
if(nfs.MountDevice(devices[0]) == NFSResult.NFS_SUCCESS)
{
if(nfs.Read("test.txt", ".", @"c:\test.txt") != NFSResult.NFS_SUCCESS)
Console.WriteLine(nfs.GetLastError());
nfs
.UnMountDevice();
}
}
nfs
.Disconnect();
}
}
}
}
}

Example 4 - Connect to NFS server, mount the first exported device and upload a file in the "test/sub" subfolder

namespace Example4
{
class Program
{
static void Main(string[] args)
{
using(NFS nfs = new NFS(NFS.NFSVersion.v2))
{
if (nfs.Connect(IPAddress.Parse("161.55.201.250")) == NFSResult.NFS_SUCCESS)
{
List devices = nfs.GetExportedDevices();
if(devices.Count > 0)
{
if(nfs.MountDevice(devices[0]) == NFSResult.NFS_SUCCESS)
{
if(nfs.Write("test.txt", "test/sub", @"c:\test.txt") != NFSResult.NFS_SUCCESS)
Console.WriteLine(nfs.GetLastError());
nfs
.UnMountDevice();
}
}
nfs
.Disconnect();
}
}
}
}
}

9 comments:

  1. I have an error with NFSResult.. vs tells me that that name cannot be found.. do you know why??

    Thanks i really need this library!

    ReplyDelete
  2. using System;
    using System.Net;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NekoDrive.NFS;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    using (NFS nfs = new NFS(NFS.NFSVersion.v2))
    {

    if (nfs.Connect(IPAddress.Parse("161.55.201.250")) == NFSResult.NFS_SUCCESS)
    {
    foreach (string device in nfs.GetExportedDevices())
    Console.WriteLine(device);
    nfs.Disconnect();
    }

    }

    }
    }
    }


    Throws a MarshalDirectiveException (Pinvoke call not valid)

    :(

    ReplyDelete
  3. Hi,
    do you have .net framework 2.0 installed?
    You must deploy nekodrive.nfs.dll nfsv2.dll and nfsv3.dll in the same folder of your application.
    What version of VS? Did you have recompiled the libraries? What version of the framework are you using?

    ReplyDelete
  4. Anyway, I'll try to reproduce this tomorrow..
    If you can, create a new project with .net framework 2.0 and try to use the library..

    ReplyDelete
  5. Ok I got it..
    first in the examples I forgot to include
    using NekoDrive.NFS.Wrappers
    second the library seems to work only if the host is a .net 2.0 or 3.0 or 3.5 project.
    With 4.0 doesn't work, maybe they changed somethig in the platform invoke (throws MarshalDirectiveException)
    Don't forget to include nfsv2.dll and nfsv3.dll as a content of your project, and select "Copy if newer" in the "Copy to Output Directory" property window.

    ReplyDelete
    Replies
    1. Hi, Is this problem "Throws a MarshalDirectiveException (Pinvoke call not valid)" resolved? I have run it in 4.0net platform and met this issue again. If you have the solution, could you please update it to me ? Thanks a lot!

      Delete
  6. Hi Mirko,

    I've coded in your examples above and it works brilliantly, however i'm trying to get it to mount as a drive letter in Windows Explorer. Is this possible?

    ReplyDelete
  7. Hi Greg,
    you can use NekoDrive.

    http://code.google.com/p/nekodrive/

    ReplyDelete
  8. Hi Mirko,

    I require your help. i want to connect to Solaris server. i used your code but no luck. i want to connect to shared path, which is been shared to my windows pc and want to connect to it and download the files on the shared path.

    Thanks,
    Manoj

    ReplyDelete