Console.WriteLine Endpoint in Console/Log - c#

So, I'm fairly certain this is a relatively remedial question with a likely simple solution, but I just can't seem to find it. I've currently got a program that writes the variables it sets to the console on execution, as well as writing them to the logfile when called through Autosys.
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Output Variable: " + variable1);
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Output Variable: " + variable2);
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Output Variable: " + variable3);
Simple stuff. But for some reason, when I call this...
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Endpoint: " + endpointAddress);
after previously setting
endpointAddress = "https://endpointaddress.com";
for debug
or
string endpointAddress = Environment.GetEnvironmentVariable("ENDPOINT");
for the Autosys call, with ENDPOINT set in the jobvars file to
JV_ENDPOINT https://endpointaddress.com
and JV_ENDPOINT set in the cmd file to
SET ENDPOINT=%JV_ENDPOINT%
with the end result of it pretty much being exactly the same as it is when the program is run through Visual Studio or the executable itself,
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Endpoint: " + endpointAddress);
is just skipped entirely in the log. The other ten or so WriteLines that are called all work fine, but nothing at all is written in that line's place.
A few lines up in the log file, I get
D:\server_apps\PROCESS_FOLDER>SET ENDPOINT=https://endpointaddress.com
showing that the endpoint is properly set, but not calling it later.
And...this just in...the most recent time I have run it, the endpoint line just WAS put into the log file. Huh. Well, let's adjust this question a bit. Would anyone happen to have any idea why this particular line might be so uniquely temperamental? I haven't changed anything significant in more than an hour, so I can't fathom why it would suddenly work. But any insight into why it may have happened (and why it might not again) could go a long way toward working any bugs or kinks out of this. Hopefully.
As always, thanks for your time!

Related

An inconsistent problem of Try-Catch in SQL command, executed in C#

I use Try-Catch to skip some error-prone SQL command in C# but I sometimes still get an execution/runtime error.
My C# code to work with a SQL server database will run frequently. On the first run, there's somewhere in my code that I try to update a table which doesn't exist yet (Later on in the code the table in discussion will be created). I put the part of query in a try-catch, but sometimes still get the error that Invalid object name 'destination_table'.
My code is like this:
string query_str =
" BEGIN TRY " +
" UPDATE [my_DB].[dbo].[destination_table] " +
" SET the_column = 'the_value' " +
" END TRY " +
" BEGIN CATCH " +
" END CATCH ";
using (SqlCommand my_command = new SqlCommand(query_str , conn))
{ form_rack_command.ExecuteNonQuery(); }
The solutions in this link seem good SQL: Try/Catch doesn't catch an error when attempting to access a table that it can't find
But my problem is that I can't always reproduce the error. It seems like sometimes when I start my computer and open Microsoft Visual Studio, I get the error on the first run of the program. But not even shutting down the system and restart everything can always cause the error.
What can be the reason that sometimes I have this problem and sometimes not?

Running remote batch file with PsExec and C#

I'm trying to run a remote batch file - already located on the remote machine - using PsExec, called via Process in C#. I've confirmed that all required files already exist, but believe I may have a problem with my syntax, as the redirected output indicates that it can't find the file specified.
The machine against which PsExec runs is dynamic, which is the myArray[0].MachineName value (this pulls in without issue).
wsStopProcess.StartInfo.FileName = #"C:\Windows\system32\PsExec.exe";
wsStopProcess.StartInfo.Arguments = #" \\" + myArray[0].MachineName + #"D:\stopprofile.bat";
wsStopProcess.StartInfo.UseShellExecute = false;
wsStopProcess.StartInfo.CreateNoWindow = true;
wsStopProcess.StartInfo.RedirectStandardOutput = true;
wsStopProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
wsStopProcess.Start();
Any ideas on what appears to be formatted incorrectly? I'm guessing it's too many backslashes (or not enough!) somewhere.
I think the main problem is you do not have a space between the two arguments.
Try this:
wsStopProcess.StartInfo.Arguments = #"\\" + myArray[0].MachineName + #" D:\stopprofile.bat";
I would also warn you that I could not get psexec to work 100%, despite trying many different things.
Try this:
wsStopProcess.StartInfo.Arguments = #"\\" + myArray[0].MachineName + #" D$\stopprofile.bat";
So instead of using : try $ sign. Also setting breakpoint on the above line while debugging will help you to see the exact path.

How can I supply a password to a console application?

As I come across issues when testing my current application, I often have to tweak the database the app uses. After losing important changes several times, I wrote a program that will back up my database to a file and then check the file into SubVersion. I have found that that backup application is not good enough.
The database is a PostgreSQL database, and my backup application invokes pg_dump to create the backup file. pg_dump is executed in a console window. When the database was on my own machine, it worked well. But when I moved the database to our test server, pg_dump asked for a password. While it's not that big a deal, I'd like to be able to supply the password automatically, since it is available in the backup application.
I've tried to follow advice I've found here, but pg_dump still stops and asks for a password. Here's the code that I thought should have supplied the password:
Process backupProcess = new Process();
backupProcess.StartInfo.UseShellExecute = false;
backupProcess.StartInfo.RedirectStandardInput = true;
backupProcess.StartInfo.FileName = dumpPath + "pg_dump.exe";
backupProcess.StartInfo.Arguments = " --host " + host +
" --port " + port +
" --username " + userName +
" --format custom --blobs --verbose --file " +
"\"" + txtBackupFile.Text + "\" " + dbName;
backupProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
backupProcess.Start();
StreamWriter standardInput = backupProcess.StandardInput;
standardInput.WriteLine("mypassword");
backupProcess.WaitForExit();// Waits here for the process to exit.
Thank you for your help.
RobR
I have solved this by using
backupProcess.EnvironmentVariables["PGPASSWORD"] = "password";
This way, I avoid the password will be stored on the computer
Maybe it's cheating a little bit (in the sense that it doesn't really help you answer the question of how to feed input to pg_dump), but you could refer to this answer which suggests the use of a .pgpass file. It would certainly be possible to write to this file dynamically rather than try to interact with the program once it prompts.
Info on .pgpass here.

Can't Access a xml file at random by C# console application

I have a C# console application which creates, parses and deletes multiple xml files at runtime. The application used to run fine in Windows 2003 server with .Net 2.0.
Recently, the Application framework was upgraded to >net 4.0 and the Windows Server OS to Windows 2008 64-bit.
Since then, the application encounters the following exception at random:
Access to the path 'D:\Content\iSDC\GDCOasis\GATE_DATA\LOG\635125008068192773\635125008074911566\SOD\AllRespId.xml' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at ProcessGateFile.SOD.saveFile(String psFile, String psXMLString, Boolean isNonAscii)
The code for the creation, parsing and deletion is as follows:
saveFile(tmpPath + "\\SOD\\AllRespId.xml", "<?xml version= \"1.0\" ?><XML>" + sbldDistinctResp.ToString() + "</XML>", isChinese);
//Save list of Distinct responsibilities for User
sbldDistinctResp.Remove(0, sbldDistinctResp.Length);
xmlCase.Load(tmpPath + "\\SOD\\AllRespId.xml");
arrResps.Clear();
//Start preparing Responsibility selection criteria
RespNodes = xmlCase.SelectNodes("//row");
sRespCriteria = "";
if (RespNodes.Count > 0)
{
foreach (XmlNode RespNode in RespNodes)
{
string RespName = RespNode.Attributes.GetNamedItem("RespId").Value.ToString();
if (!arrResps.Contains(RespName))
{
arrResps.Add(RespName);
}
}
for (int i = 0; i < arrResps.Count; i++)
{
sbldDistinctResp.Append("(#RespId = '" + arrResps[i].ToString() + "') or ");
}
sbldDistinctResp.Remove(sbldDistinctResp.Length - 4, 4);
sRespCriteria = sbldDistinctResp.ToString();
if (!sRespCriteria.Equals(""))
{
sRespCriteria = "(" + sRespCriteria + ")";
}
}
File.Delete(tmpPath + "\\SOD\\AllRespId.xml");
I repeat, the error is happening at random, i.e. it works at times and does not at other times during the same process.
Any idea what might be causing this and how to resolve?
Just a couple of observations:
Why are you saving and then immediately loading the file again? In fact, why do you even need to save this file - you already have all the information you need in the sbldDistinctResp variable to generate the XML you need to work with (as evidenced by the saveFile call at the start of the code) - couldn't you just make a copy of it, surround it with the same XML as you did during saveFile, and work with that?
"It happens randomly" is a very subjective observation :). You should profile this (run it 10,000 times in a loop for example) and record the pattern of errors. You may well be surprised that what seems random at first actually shows a clear pattern over a large number of runs. This may help you to make a connection between the problem and some other apparently unrelated event on the server; or it may confirm that it truly is random and therefore outside of your control.
If you really can't find the problem and you go with the idea of anti-virus, etc, then you could wrap the loading code in a try/catch and re-try a couple of times if you get the error. It's hacky but it would work, assuming you have accepted that the initial error is beyond your control.

C# Using Reshacker to change executable's Icon

There are MULTIPLE threads here asking how to change an icon - and nearly all of them say to use a command line tool such as ResHacker - but none of them (that I have seen) explain how to do so. I read into ResHacker's help file, and I found some text which explained how to go about changing the icon of a win32 executable file.
I tried the below code, and it gave me the following error:
Code:
p.StartInfo.Arguments = "-addoverwrite " + txtProtect.Text + "," + txtProtect.Text + "," + sICOpath + "," + "ICONGROUP" + ", MAINICON, 0";
Error:
"C:\Users\Evan\Desktop\ResHacker.exe" -addoverwrite C:\Users\Evan\Desktop\output.exe,C:\Users\FARINA_EVAN\Desktop\output.exe,C:\Users\Evan\Desktop\ExeWithIcon.exe,ICONGROUP, MAINICON, 0
Error: Invalid resource type.
I realize that this is a bit... Old, but the reason this won't work is because you're trying to get the icon FROM a .exe, which doesn't work with the command line of ResHacker.
The only thing I can think of is to Extract the icon from the .exe and save it as a .ico.
Then you can do the "push."
I wasn't able to find anything on how to programmatically run ResHacker except through manipulating the command line through C# like you are attempting to do. However, to speak to the root of your question, I found a possible solution for you here that does not require ResHacker. Instead, it allows you to modify the icon through code (C# and VB.NET). Here is the link:
http://www.hackforums.net/archive/index.php/thread-422072-1.html

Categories