简体   繁体   中英

unhandled exception c# dll

I tring to test a new dll that I've build for c#

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

but I get the following error at String [] first = UserQuery.Get_All_Users();

An unhandled exception of type 'System.NullReferenceException' occurred in User_Query.dll

Additional information: Object reference not set to an instance of an object.

I been tring to figure this one out for hours but can't find any null varibles

I post my dll in case the dll is wrong

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;
            }           
        }
    }
}

There is a problem with your. path variable... since there should be \\\\ instead of //

The problem here turned out not to be the code but be VS2010 not loading the dll. This happen because I decided to change the program from using the dll from the debug to the release version but I did not clean the project after doing it and therefore the program was not correctly loading the dll. All that need to be done was clean the project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM