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.
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.
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.
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
精彩评论