开发者

List with items returns empty

I have created a simple List function but if I Loop through the List it's empty. It should not be!

// List function 
    public class process_hook
    {
        public static List<String> pro_hook = new List<String>
                                              (new String[] { list_all_pocesses() });
        protected static string list_all_pocesses()
        {
            StringBuilder _list = new StringBuilder();
            foreach (Process i in Process.GetProcesses("."))
            {
                try
                {
                    foreach (ProcessModule pm in i.Modules)
                    {
 开发者_Go百科                       pro_hook.Add(pm.FileName.ToString());
                    }
                }
                catch { }
            }
            return _list.ToString();
        }
    }


        // call 
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (String _list in process_hook.pro_hook)
            {
                Console.WriteLine(_list);
            }
        }


Well this is a problem to start with:

catch { }

If anything goes wrong, you'll just silently abort.

Maybe that's what's happening? (EDIT: It is. See later.)

The next problem is that your "list" will only ever contain a single string... is that really what you intended? I doubt that the list you're seeing is actually empty - but it will contain a single empty string.

(As a side note, I would strongly suggest that you start following .NET naming conventions and avoid global variables like this.)

EDIT: Aargh - I've just realized what you've done. You're probably actually getting a NullReferenceException in list_all_pocesses, which you've caught and ignored.

Your call to pro_hook.Add is made before you've assigned a value to pro_hook. Basically you've got a variable initializer which uses a method which in turn uses the variable. Don't do that. If you step through your code in the debugger, you may get more of an idea of what's going on, but basically you've created a big ball of spaghetti for yourself.

Why doesn't list_all_pocesses just return a List<string>? Why are you using a StringBuilder at all?


Well... you're returning an empty string builder. That's your problem. Your code is doing what you're telling it to do. :)

 return _list.ToString();


public class process_hook
{
    public static List<string> pro_hook = list_all_pocesses();
    protected static List<string> list_all_pocesses()
    {
        List<string> list = new List<string>();

        foreach (Process i in Process.GetProcesses("."))
        {
            foreach (ProcessModule pm in i.Modules)
            {
                list.Add(pm.FileName.ToString());
            }
        }
        return list;
    }
}


_list.ToString() is not going to return any meaningful value. Try something like this instead:

public static List<string> pro_hook = list_all_processes();

protected static List<string> list_all_processes()
{
    var list = new List<string>();

    foreach (Process i in Process.GetProcesses(".")) {
        try {
            foreach (ProcessModule pm in i.Modules) {
                list.Add(pm.FileName);
            }
        } catch { }
    }

    return list;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜