である私は私のローカルデバイスXに(のは、\サーバー\フォルダを言わせて)共有を接続しようとしている:でWNetAddConnection2は、エラー1200を返します - ローカル名が有効
[DllImport("Mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int WNetAddConnection2(
[In] NetResource lpNetResource,
string lpPassword,
string lpUsername,
int flags
);
public static bool Connect(string remoteName, string localName, bool persistent) {
if (!IsLocalPathValid(localName)) return false;
var r = new NetResource
{
dwScope = ResourceScope.RESOURCE_GLOBALNET,
dwType = ResourceType.RESOURCETYPE_ANY,
dwDisplayType = ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE,
dwUsage = ResourceUsage.RESOURCEUSAGE_CONNECTABLE,
lpRemoteName = remoteName,
lpLocalName = localName
};
return WNetAddConnection2(r, null, null, persistent ? 1 : 0) == 0;
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource {
public ResourceScope dwScope;
public ResourceType dwType;
public ResourceDisplayType dwDisplayType;
public ResourceUsage dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
Connect(@"\\server\folder", "X:", true);
を呼び出すとき関数は単にfalseを返します - エラーは1200(BAD_DEVICE)と表示されます。 NETRESOURCEは次のようになります。私はすでにいくつかのスニペット(のPInvoke)で確認
lpRemoteName = "\\\\server\\folder";
lpProvider = null;
lpLocalName = "X:";
lpComment = null;
dwUsage = Connectable;
dwType = Any;
dwScope = GlobalNet;
dwDisplayType = Share;
は、私は何の違いを見ることはできません置きます。たぶん、あなたはこの謎を解くことができます...
EDIT1
ローカル名を指定しないとマップドライブは機能しますか?つまり、 'localName'を空の文字列にします。これは他の問題があるかどうかの手がかりを与えるかもしれません。エラーコード1200は、あなたの "X:\"に正しくないものがあることを意味します。余分なスペースがあります(https://stackoverflow.com/questions/31137879/excel-to-sharepoint-mapnetworkdrive-errorを参照してください) – Subbu
残念ながら、ローカルドライブが指定されていなくてもエラー1200が続く(空文字列)。さらなるNetResourceの詳細については、私のEdit1を参照してください。 (私は、サーバー名をクリアしなければならなかった、申し訳ありません。 – C4p741nZ