2017-08-24 23 views
0

對不起,有些可能是非常基本的問題。我只是想用一個字符串替換UNC路徑。這些線與用於C中的UNC路徑++連接完美的工作原理:嘗試用字符串替換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 
); 

爲你的想法很多感謝..

+0

可能重複[傳遞一個自定義屬性作爲參數的變量值](https://stackoverflow.com/questions/13125046/passing-a-custom-attribute-with-a-variable-value-as- a參數) – Diado

回答

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 
); 
+2

或者換句話說,如果使用'@'前綴,則不需要轉義反斜槓。 – stuartd

+0

工作..謝謝。 – Sepp

0

您正在嘗試使用非字符串常量與屬性,這是不允許的。你必須聲明你的字符串爲「const」。

相關問題