string dirPath=“\\192.168.123.10\项目文件夹\上海市~1\大基地~1\\曹路大~1\建房成本\\2013-5\\详细规~1\\上报文~1\\上报文~1\\上报文~1\”
DirectoryInfo dir = dir = new DirectoryInfo(dirPath);
会报:
指定的路径或文件名太长,或者两者都太长。完全限定文件名必须少于 260 个字符,并且目录名必须少于 248 个字符。
已经用的是短路径了,为什么还会这样?原始的长路径的确会超长,有270个字符
------解决思路----------------------
调用API来实现也可以
public class NetResourceManager
{
/// <summary>
/// 网络资源管理器
/// </summary>
/// <param name="remoteResrc">远程资源URL,如果是IP v6的机器,就用-替代:分隔
/// 如\\2001:4898:9:3:c069:aa97:fe76:2449\share就表示为\\2001-4898-9-3-c069-aa97-fe76-2449.ipv6-literal.net\share
/// </param>
/// <param name="localPath">本地映射的盘符(不带反杠),如X:</param>
public NetResourceManager(string remoteResrc, string localPath)
{
this._netResrc = new NetResource(remoteResrc, localPath);
}
public bool ConnectTo(string domainUser, string pwd)
{
int result = NetResourceManager.WNetAddConnection2(this._netResrc, pwd, domainUser, CONNECT_UPDATE_RECENT_TEMPORARY);
if (result == 0)
{
return true;
}
SamosService.Common.WinAPI.Win32Invoker.TraceWin32Error("ConnectTo", "WNetAddConnection2");
return false;
}
public bool CancelConnection()
{
if (!string.IsNullOrEmpty(this._netResrc.RemoteName))
{
int result = WNetCancelConnection2(this._netResrc.RemoteName, 0, true);
if (result == 0)
{
return true;
}
SamosService.Common.WinAPI.Win32Invoker.TraceWin32Error("CancelConnection", "WNetCancelConnection2");
}
return false;
}
#region 网络资源映射 WIN32 API
/// <summary>
/// 资源类型:磁盘文件
/// </summary>
public const int RESOURCETYPE_DISK = 1;
/// <summary>
/// 网络资源的连接选项:不显示在最近连接列表
------解决思路----------------------
临时连接
/// </summary>
public const uint CONNECT_UPDATE_RECENT_TEMPORARY = 0x0002
------解决思路----------------------
0x0004;
/// <summary>
/// 添加指定资源的网络映射
/// </summary>
/// <param name="lpNetResource">网络资源对象</param>
/// <param name="lpPassword">密码</param>
/// <param name="lpUsername">域\用户名,没有域时只需要用户名</param>
/// <param name="dwFlags">连接选项</param>
/// <returns></returns>
[DllImport("mpr.dll", SetLastError = true)]
public static extern int WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
[DllImport("Mpr.dll", SetLastError = true)]
public static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);
[StructLayout(LayoutKind.Sequential)]
public struct NetResource
{
public NetResource(string remoteResrc, string localPath)
{
this.dwScope = 1; //1:可连接,2:可枚举所有,3:记住连接(持久连接)
this.dwType = NetResourceManager.RESOURCETYPE_DISK;
this.dwDisplayType = 3; //RESOURCEDISPLAYTYPE_SHARE
this.dwUsage = 1;
this.LocalName = localPath; //若指定值,将重定向本地设备
this.RemoteName = remoteResrc;
this.Comment = null;
this.Provider = null;
}
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPStr)]
public string LocalName;
[MarshalAs(UnmanagedType.LPStr)]
public string RemoteName;
[MarshalAs(UnmanagedType.LPStr)]
public string Comment;
[MarshalAs(UnmanagedType.LPStr)]
public string Provider;
}
#endregion
private NetResource _netResrc;
}
上面是临时映射,你可以改成持久连接(用户登陆时自动连接)
将CONNECT_UPDATE_RECENT_TEMPORARY改为
CONNECT_UPDATE_PROFILE = 0x0001;