views:

103

answers:

3

I have a C# project that compiles normally. When I run the project via Visual Studio's IDE it runs fine and ends cleanly. However when I navigate to the project directory and attempt to run the program by double clicking the "exe" file or referencing it via the run window it errors out.

I have narrowed the problem down to

Console.WriteLine("output ->" + any_variable);

For some reason if I print any variable using the console.writeline the application will error if ran as described earlier.

If I take this line out, the executable created by the Visual Studio will run just fine if i double click on it. I'm really confused by this. My goal here is to create this command line project as a scheduled task.

+1  A: 

I'm sorry, I was wrong... so EDIT

Place your code into try{} catch{} construct:

try
{
//your code goes here
}
catch (Exception ex)
{
    Console.WriteLine("An error occured: {0}", ex.Message);
    if (ex.InnerException != null)
       Console.WriteLine("Inner Exception: {0}", ex.InnerException.Message);
    Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
}

This is neccessary to find out were is your problem, at which line of code etc.

Eugene Cheverda
While I agree with your observation, why would that make a difference between running inside IDE and outside? It should actually give you a compile-time error.
Abel
Actually the second argument to the string `+` operator can be any type. String will call the `ToString()` automatically.
Ron Warholic
Ah, now I feel stupid, you're right! I should stop mixing languages :)
Abel
(after your EDIT ;-) If `Console.WriteLine` doesn't work, as the OP implies, you should use MessageBox, or simply attach to the process to avoid all this hassle of script-style debugging (see also my answer, which explained all this in more detail).
Abel
+3  A: 

I'd assume the error not having anything to do with WriteLine or even a Console. While a common a difference between running from IDE and running from double-clicking might be the rights (i.e., you may start the IDE as admin, or the location that you are writing/reading from/to is different and has different ACLs attached to it), thiis doesn't seem to be the case here.

To catch your actual error, compile into debug mode. Start your application (possibly with a message box of some kind). Start the IDE and select Debug and Attach to process (you have all the time if you pause your app with a message box). Select your process. Run until you receive the error. You should now receive the error in the IDE, even though the app is run from double clicking the EXE. You can see the stack and debug as you would normally do.

My guess? The variable you're printing does something that raises the error.

Altenatively: a simple try/catch around the offending statement plus a messagebox with the Exception.Message should give you some more intel too.

Abel
Thanks, turns out it was a problem with a project I had referenced. After catching the exception that error was clear. I'm not sure why the referenced project isn't being applied to the .exe file while working fine in the IDE, but I'll keep searching. If anyone might know the answer to that it would also help greatly.
Ryan
@Ryan, that new information sounds excellent for a new question. If you consider this or another to be the answer to your (original) question, make sure to mark it as such (the big green checkmark on the left), you'll even earn points in doing so. Welcome at SO! And have a look here for some hints: http://stackoverflow.com/faq
Abel
A: 

I don't know if this applies to your case or not, but I had a very similar issue that I posted here the other day: http://stackoverflow.com/questions/3296081/weird-error-message-when-running-my-application

icemanind