欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 判断另外一台服务器上某个文件是否存在

程序员文章站 2022-06-10 20:27:27
...

System.IO.File.Exist(filePath)在本地测试B服务器上文件是否存在 通过。放在A服务器上执行 测试B服务器文件是否存在失败。

解决方案:远程主机共享盘映射到本地

 #region 网络共享本地映射
 public class WNetHelper
 {

     [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
     private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
     [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
     private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
     [StructLayout(LayoutKind.Sequential)]
     public class 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;

     }
     /// <summary>  
     /// 为网络共享做本地映射
     /// </summary>  
     /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>
     /// <param name="password">访问用户密码</param>  
     /// <param name="remoteName">网络共享路径(如:\\fs-01\暂存文件夹 (一定要路径,不可以带文件名全路径)</param> 
     /// <param name="localName">本地映射盘符</param>  
     /// <returns></returns>  
     public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
     {
         NetResource netResource = new NetResource();
         netResource.dwScope = 2;
         netResource.dwType = 1;
         netResource.dwDisplayType = 3;
         netResource.dwUsage = 1;
         netResource.lpLocalName = localName;
         netResource.lpRemoteName = remoteName.TrimEnd('\\');
         uint result = WNetAddConnection2(netResource, password, username, 0);
         return result;

     }
     public static uint WNetCancelConnection(string name, uint flags, bool force)
     {
         uint nret = WNetCancelConnection2(name, flags, force);
         return nret;
     }
 }
 #endregion