1

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.

2
  • If you run the app from a command prompt, it should print out the stack trace if it's running in debug mode. Should give you more of an idea, post the stack trace if not. (And remove the asp.net tag from the question) Commented Jul 21, 2010 at 21:06
  • Please check my update and provide us either more code or exception details. Commented Jul 21, 2010 at 21:34

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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, 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: stackoverflow.com/faq
1

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.

4 Comments

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.
Actually the second argument to the string + operator can be any type. String will call the ToString() automatically.
Ah, now I feel stupid, you're right! I should stop mixing languages :)
(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).
0

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: Weird error message when running my application

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.