簡體   English   中英

未處理的異常C#dll

[英]unhandled exception c# dll

我試圖測試為C#構建的新dll

private void button1_Click(object sender, EventArgs e)
        {
            String [] first = UserQuery.Get_All_Users();
            //MessageBox.Show(first);
        }

但我在String [] first = UserQuery.Get_All_Users();遇到以下錯誤: String [] first = UserQuery.Get_All_Users();

User_Query.dll中發生了類型為'System.NullReferenceException'的未處理的異常

附加信息:對象引用未設置為對象的實例。

我一直想找出一個小時,但找不到任何空變量

我張貼我的dll以防dll錯誤

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace User_Query
{
    public class UserQuery
    {
        public static string[] Get_All_Users()
        {
            string[] names = new string[10];

            var path = string.Format("WinNT://{0},computer", Environment.MachineName);

            using (var computerEntry = new DirectoryEntry(path))
            {
                var userNames = from DirectoryEntry childEntry in computerEntry.Children
                                where childEntry.SchemaClassName == "User"
                                select childEntry.Name;
                byte i = 0;
                foreach (var name in userNames)
                {
                    Console.WriteLine(name);
                    names[i] = name;
                    i++;
                }
                return names;
            }           
        }
    }
}

您的有問題。 路徑變量...,因為應該有\\\\而不是//

原來這里的問題不是代碼,而是VS2010未加載dll。 發生這種情況的原因是,我決定將程序從調試使用的dll更改為發行版,但是在執行此操作后我沒有清理項目,因此程序無法正確加載dll。 所有需要做的就是清理項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM