aroky2010 2023-03-27 10:41 采纳率: 33.3%
浏览 165
已结题

window下挂载linux环境的NFS的共享目录

各位好,小猿现在有个头大的问题。
问题描述:在Linux环境下部署NFS的服务,并添加一个共享目录。例如:linux机器的IP:192.168.58.16 NFS的共享目录为:/home/nfs_test。
在linux环境部署成功后,可以通过


showmount -e 192.168.58.16  //命令能成功查询到该目录

现在需要通过windows的机器,IP:192.168.58.84去挂载linux机器的NFS的共享目录到Q:盘 。
我本人当前使用了

CreateProcess   net use Q: \\\\192.168.58.16\\home\\nfs_test
WNetAddConnection2

以上的方式。但是都不成功。

现在需要有大猿帮忙实现这个功能,提供可用的源码和demo,谢谢。

  • 写回答

9条回答 默认 最新

  • 海洋 之心 2022年度博客之星人工智能领域TOP 1 2023-03-27 10:45
    关注

    以下是一个实现在 Windows 系统中挂载 Linux NFS 共享目录的 C# 示例代码:

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace MountNfsShare
    {
        class Program
        {
            [DllImport("mpr.dll", CharSet = CharSet.Auto)]
            public static extern int WNetAddConnection2(
                ref NETRESOURCE lpNetResource,
                string lpPassword,
                string lpUsername,
                int dwFlags);
    
            [StructLayout(LayoutKind.Sequential)]
            public struct NETRESOURCE
            {
                public int dwScope;
                public int dwType;
                public int dwDisplayType;
                public int dwUsage;
                public string lpLocalName;
                public string lpRemoteName;
                public string lpComment;
                public string lpProvider;
            }
    
            static void Main(string[] args)
            {
                // Linux NFS 共享目录的 IP 和路径
                string nfsServerIp = "192.168.58.16";
                string nfsSharePath = "/home/nfs_test";
    
                // 本地 Windows 目录名,可以自定义
                string localDriveName = "Q:";
    
                // 挂载 Linux NFS 共享目录到本地 Windows 目录
                if (MountNfsShare(nfsServerIp, nfsSharePath, localDriveName))
                {
                    Console.WriteLine("Linux NFS share mounted successfully.");
                }
                else
                {
                    Console.WriteLine("Failed to mount Linux NFS share.");
                }
    
                Console.ReadKey();
            }
    
            static bool MountNfsShare(string nfsServerIp, string nfsSharePath, string localDriveName)
            {
                // 构造 NETRESOURCE 对象,用于映射网络驱动器
                NETRESOURCE netResource = new NETRESOURCE();
                netResource.dwScope = 2;
                netResource.dwType = 1;
                netResource.dwDisplayType = 0;
                netResource.dwUsage = 0;
                netResource.lpLocalName = localDriveName;
                netResource.lpRemoteName = $"\\\\{nfsServerIp}\\{nfsSharePath}";
                netResource.lpComment = null;
                netResource.lpProvider = null;
    
                // 调用 WNetAddConnection2 函数,映射网络驱动器
                int result = WNetAddConnection2(ref netResource, null, null, 0);
                if (result == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    

    上述代码中,我们使用了 WNetAddConnection2 函数来挂载 Linux NFS 共享目录到本地的 Windows 目录。

    可以将上述代码保存为一个 C# 项目,然后在 Visual Studio 中编译并运行,即可实现在 Windows 系统中挂载 Linux NFS 共享目录的功能。

    需要注意的是,为了能够成功挂载 NFS 共享目录,需要确保 Windows 系统上已经安装了 NFS 客户端。另外,如果 Linux 系统的 NFS 服务使用了访问控制列表(ACL),需要在 exports 文件中将 Windows 系统的 IP 地址添加到 ACL 中。

    评论

报告相同问题?

问题事件

  • 系统已结题 4月4日
  • 创建了问题 3月27日