尝试用字符串替换UNC路径时出错

时间:2017-08-24 11:53:16

标签: c#

对不起有些非常基本的问题。我只想用字符串替换UNC路径。具有C ++连接的UNC路径的这些行完美地起作用:

[DllImport(C:\\Users\\SJ\\Documents\\VS2015\\Projects\\P_01\\Debug\\EV_01.dll",
EntryPoint = "DDentry", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void DDentry
(
   [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
    string[,] pArrayStr
);

用字符串替换UNC路径会产生错误"非静态字段,方法或属性需要对象引用"

string UNCpath = @"C:\\Users\\SJ\\Documents\\VS2015\\Projects\\P_01\\Debug\\EV_01.dll";

[DllImport(UNCpath,
EntryPoint = "DDentry", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void DDentry
(
   [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
    string[,] pArrayStr
);

非常感谢您的想法..

2 个答案:

答案 0 :(得分:1)

您无法将实例值UNCPath传递给类似的属性。它需要是一个常数。此外,如果使用双反斜杠转义序列,则不能对字符串使用@前缀。

试试这个:

const string UNCpath = "C:\\Users\\SJ\\Documents\\VS2015\\Projects\\P_01\\Debug\\EV_01.dll";

[DllImport(UNCpath,
EntryPoint = "DDentry", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void DDentry
(
   [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
    string[,] pArrayStr
);

答案 1 :(得分:0)

您正在尝试使用具有属性的非常量字符串,但不允许这样做。您必须将字符串声明为" const"。