2012-02-20 71 views
0

好了,所以我試圖從Active Directory用戶拉入一個DirectoryUser對象,如果我喜歡這種類型,它工作得很好:DirectoryEntry字符串作爲文字而不是格式化字符串?

DirectoryEntry user = new DirectoryEntry(@"LDAP://CN=Name, OU=Department, OU=Group, DC=Domain1, DC=Domain2"); 
user.Properties["thumbnailPhoto"].Clear(); 

但我需要的價值才能夠被改變,所以我嘗試了格式化字符串:

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2); 
DirectoryEntry user = new DirectoryEntry(ldap); 
user.Properties["thumbnailPhoto"].Clear(); 

但是,這會導致錯誤「有在服務器上沒有這樣的對象」

構造函數的字符串,而我通過,我是完全相同的值使用時,我真的寫了,爲什麼它以一種方式而不是另一種方式工作?!

編輯: 我只是想補充一點,我在幾個不同的方式雙重檢查,以驗證內置字符串中走出等同於硬編碼字符串,它是。我跑過調試器,並檢查user.path值以驗證字符串是否存儲完全相同。到目前爲止,所有東西都是相同的,但一個是有效的,另一個不是!

更新: 我注意到,如果我硬代碼直接給一個字符串變量:

string ldap = @"Jeremy Stafford", "IT", "QGT", "QGT", "Local"; 
DirectoryEntry user = new DirectoryEntry(ldap); 

這一切正常。這導致我相信字符串「type」可能有問題。也許如果我可以將構建的字符串轉換回原始字符串(或者相對於引用類型而言是一個值類型),它會起作用嗎?但我不知道該怎麼做。有任何想法嗎?

更新: 我運行了語義測試。下面是我使用的代碼:

string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);     
       var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local"; 
       string message; 

       if (ldapFormatted.Equals(ldapHardCoded)) 
       { 
        message = "They're the same value\n"; 
       } 
       else 
       { 
        message = "Strings are not the same value\n"; 
       } 

       if (ldapFormatted.GetType() == ldapHardCoded.GetType()) 
       { 
        message += "They are the same type"; 
       } 
       else 
       { 
        message += "They are not the same type"; 
       } 
       message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded; 
       MessageBox.Show(message); 

這裏是結果:

enter image description here

+0

道歉,我用它們作爲佔位符。我已經更新了OP,以顯示我通過的實際值。 – Sinaesthetic 2012-02-21 15:46:50

+0

我覺得我應該爲我糟糕的打字道歉。 _ /指責technology_ – 2012-02-21 15:49:43

+1

請出示用來初始化變量代碼:'CN,組,OU,域1,domain2' – sll 2012-02-21 16:43:25

回答

2

我想你的代碼,並填寫格式變量,它爲我工作,你可以在截圖中看到。

string cn = "Jeremy Stafford"; 
    String group = "IT"; 
    string ou = "QGT"; 
    String domain1 = "QGT"; 
    string domain2 = "Local"; 
    string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2); 

    var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local"; 
    string message; 

    if (ldapFormatted.Equals(ldapHardCoded)) 
    { 
    message = "They're the same value\n"; 
    } 
    else 
    { 
    message = "Strings are not the same value\n"; 
    } 

    if (ldapFormatted.GetType() == ldapHardCoded.GetType()) 
    { 
    message += "They are the same type"; 
    } 
    else 
    { 
    message += "They are not the same type"; 
    } 
    message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded; 
    MessageBox.Show(message); 

Message

你真的應該檢查什麼在你的輸入變量。也許你有一些看不見的字符。將輸入變量轉換爲字節數組並打印出數組以獲得更多信息。

+0

我繼續測試這些變量,並且它們保持失敗。它確定,但。我們找到了另一種方式去了解更大的局面,這似乎正在起作用。 – Sinaesthetic 2012-02-21 17:20:54

0

嗯,你爲什麼不嘗試在硬編碼的String.Format的值,如果你的作品你的變量有問題。

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", "Name", "Department", "Group", "Domain1", "Domain2"); 

除此之外,我不認爲它爲什麼不會與硬編碼和沒有工作,他們兩個都是字符串,畢竟。

+0

就像我說的,我比較了字符串和'是相同的。只是爲了S&G,我努力在string.format中編碼值,並得到了相同的錯誤。 – Sinaesthetic 2012-02-21 15:36:19

1

我相信硬編碼字符串有些不妥,因爲如果您將硬編碼值複製到變量cn, group, ...中,您就有工作測試。所以請分享一個初始化變量的代碼cn, group, ou, domain1, domain2

下面的測試代碼正常工作。

string cn = "Jeremy Stafford"; 
string group = "IT"; 
string ou = "QGT"; 
string domain1 = "QGT"; 
string domain2 = "Local"; 

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
          cn, group, ou, domain1, domain2);  
string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
             cn, group, ou, domain1, domain2);     
var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local"; 

bool bothTheSame = ldapFormatted.Equals(ldapHardCoded) 
        && 
        ldapFormatted.GetType() == ldapHardCoded.GetType();