service程序在映射出来的文件夹创建Folder:cannot find a part of the path
server里面share了一个folder,我把它映射到本地S:
然后在service程序里,我想在该文件夹里创建文件夹,用Directory Create,路径是S:\\a,
error:Could not find a part of the path 'S:\a'
我尝试用UNC Path,就是'\\server\\sharefolder\\a',报错
error:Access to the path '\\server\\sharefolder\\a' is denied.
在网上查了半天,都说是权限问题,但是没找到哪儿的权限除了问题。
PS: 我另外在console程序里尝试了一下create,OK 没问题。但是service程序不行。
跪求大神指点。 service
[解决办法]
建议你建一个专用的域账号并给他分配权限,然后设成密码不可修改、永不过期什么的,比直接用自己的好。
然后:
a.把service的运行账号改成这个域账号
或者
b.在代码里访问网络文件夹的前后加上身份模拟
下面这个是我一直用的,或者你google下别人的代码(C# 身份模拟)
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPStr)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;
[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;
}
[DllImport("mpr.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetAddConnection2A(
[MarshalAs(UnmanagedType.LPArray)]
NETRESOURCE[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)]
string lpPassword,
[MarshalAs(UnmanagedType.LPStr)]
string lpUserName,
int dwFlags);
[DllImport("mpr.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetCancelConnection2A(
[MarshalAs(UnmanagedType.LPStr)]
string lpName,
int dwFlags,
int fForce);
public static bool Connect(string remotePath, string user, string pwd)
{
NETRESOURCE[] netRes = new NETRESOURCE[1];
netRes[0].lpRemoteName = remotePath;
netRes[0].lpLocalName = null;
netRes[0].dwType = 1;//disk
netRes[0].dwDisplayType = 0;
netRes[0].dwScope = 0;
netRes[0].dwUsage = 0;
netRes[0].lpComment = "";
netRes[0].lpProvider = null;
DisConnect(remotePath);
int iRes = WNetAddConnection2A(netRes, pwd, user, 0);
if (iRes != 0)
Log.GetInstance().LogFatal(string.Format("Can not connect to file server:{0}, error code:{1}", remotePath, iRes));
return iRes == 0;
}
public static bool Disconnect(string remotePath)
{
int iRes = WNetCancelConnection2A(remotePath, 0, 1);
if (iRes == 0
[解决办法]
iRes == 2250) //2250 means connection not exists
return true;
return false;
}
if (Connect(path, user, pwd))
{
try {...}
catch {...}
finally
{
Disconnect(path);
}
}
好像是同一个网络文件夹地址,在一台机器上,同一个时间只能有一个账户的active连接(同一个账户可以连多个,只是说一个人连了,别人的就不能再连,好像是这样)。
比如你有一个资源管理器窗口正开着,那么这个就是一个用你账户的active的连接,而这时候你调试代码里面用另一个账户去连,就可能会报错。