views:

314

answers:

2

Hi all,

I've build a program in c# windows forms, now on the first load up it trys to create a scheduled tasks. if it raises exception and it's main computer then this is the first time the softwere loads(you can intall this program on many computer but one computer is the main with the scheduled tasks).

i've try this program on many computer and it worked perfectly(xp-sp1/2, vista-sp1/2, win7), now when i try to install it on a limited user(on win xp pro sp2) when it trys to create the scheduled tasks - i get a Argument Null Reference, and when i enter the admin user, it's install the scheduled task on the admin user and won't run if the limited user logged in(which is 99.9% of the time)...any ideas why i get this exception?? i've looked hours on the code to search where the exception come from but i can't find it! any one have ideas?

Thanks alot!

Amit.

MainOrSec = true; User, Pass are public varb whice return from FirstTimeUp.

private bool CreateNoExit()
        {
            try
            {
                RegistryKey key = Registry.CurrentUser;
                key = key.OpenSubKey("Crm");
                MainOrSec = Convert.ToBoolean(AESIMP.Decrypt((string)key.GetValue(AESIMP.Encrypt("MorS"))));
            }
            catch (ArgumentNullException)
            {
                MainOrSec = true;
            }
            if (MainOrSec)
            {
                ScheduledTasks sc = new ScheduledTasks();
                Task task;
                try
                {
                    task = sc.CreateTask("NoExit");
                    FirstTimeUp f = new FirstTimeUp(this);
                    f.ShowDialog();
                }
                catch (ArgumentException)
                {
                    return false;
                }
                if (!CreatT)
                    return false;
                task.ApplicationName = @"C:\Program Files\Triffon\Crm Setup
2.0.0002\noexit.exe";
                task.Comment = "Check For no exit on the database.";
                task.SetAccountInformation(User, Pass);
                task.IdleWaitMinutes = 10;
                task.Triggers.Add(new DailyTrigger(5, 0));
                try
                {
                    task.Save();
                    task.Close();
                    sc.Dispose();
                }
                catch (COMException ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }
                return true;
            }
            return false;
        }
+4  A: 

It's hard to figure this out without a stack trace, but there is a suspicious line of code.

According to MSDN RegistryKey.GetValue() returns:

The value associated with name, or a null reference (Nothing in Visual Basic) if name is not found.

Here you pass the result of that function directly to another function:

MainOrSec = Convert.ToBoolean(AESIMP.Decrypt((string)key.GetValue(AESIMP.Encrypt("MorS"))));

Try to call it in a few steps instead, checking for null where needed:

string s = key.GetValue(AESIMP.Encrypt("MorS")) as string;
if(!string.IsNullOrEmpty(s))
    MainOrSec = Convert.ToBoolean(AESIMP.Decrypt(s));
else
    MainOrSec = true;
Sander Rijken
Thanks. but that why i putted it in "try-catch", on first load it triggers the null exception and then it enters the Configuration Manger of the program, there i solve the exception for next load...the problem is more about permission but thanks for trying help me
Mazki516
+4  A: 

OK, so if you get an exception, the best thing to do is to run your program under Visual Studio's debugger so you can see exactly where the exception is called. Here, Ctrl-Alt-E is your friend: turn on the checkbox in the "Thrown" column next to "Common Language Runtime Exceptions" and you'll break to the debugger no matter what.

If you are testing your application on a user's computer without Visual Studio then you have some other options. One (if you're using Pro and above) is to run the Remote Debugger on the remote PC. Then you can attach to the running program and see the exception.

If you don't have Pro, or can't easily use the remote debugger, then it is definitely worth using a decent logging framework like log4net to make sure that all exceptions are caught, trapped, and written to a log file. Frankly no production application should be released until this is done.

When you've done this, take a careful look at the exception trace to see where the problem is caused. I'd be willing to bet that that ScheduledTasks class is throwing an exception somewhere that you're not expecting.

Finally, you'll be getting downvotes because the culture here is "we'll help if you let us know everything we need to know to help." There's been a couple of requests in the comments for the full stack trace, which hasn't appeared, so people here will consider that rude, I'm afraid.

Jeremy McGee
you are right, didn't mean to sound that way but you really need to see me seating on a seat writing this softwere to a buissnes and then i get this exception where thay have 30 computers with a limited user...finally i found a solution, you install the task with admin username and password and then give ntfs permissions to those task...it's solved it out...took me hours to write those nfts permissions, but at lease it's working :) thanks for the info about Remote debugger. never heard of it! Thanks!
Mazki516