I am writing a program to function as a testing plugin with a third party program. I have talked with the developer on the other end (don’t get me started) and he is using VB 6 to create a batch file and then calls the batch file using a shell command. His batch file is calling my plugin which I have written to output data to a file based on the parameters that I receive via his batch file.
My problem is that when his program calls the batch file via a shell command (again, he is using VB 6), my program executes and the console writelines that I put in place in front of and after the filestream stuff all execute, but the files are not created and therefore his program does not read the file I create (CP07.txt in this case). If I manually run the batch file from Windows explorer everything works as expected. I do have a process monitor capture if that is beneficial. The program is running as the local admin on a Win7 machine (although it is not elevated), UAC is disabled, there is no anti-virus software installed, and it is not writing to a root or system folder.
I appreciate any pointers. Since the ProcMon capture is long (1400+ lines), I have not posted it.
Regards,
S
Here is a sample batchfile from the other program…
- Full path to my exe
- Full path to the output file
- Parameter for my use
- Username
-
Password
C:\SpecifiedDir\myprogram.exe C:\Specified\Directory\CP07.txt parameter username password
I am overwriting the output file parameter as you can see because it did not work initially the other way (I think maybe something to do with the slashes being escaped or something) and I wanted to start with a simple and working program and then clean it up from there.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Plugin
{
class Program
{
static void Main(string[] args)
{
Console.Write("Press any key to begin: ");
Console.Write("Starting...", Console.ReadLine());
//Console.WriteLine("Done");
Console.WriteLine("Number of command line parameters = {0}",args.Length);
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
Console.WriteLine("");
TextWriter defaultOutputMethod = Console.Out;
TextWriter defaultErrorOutputMethod = Console.Error;
FileStream fStream;
StreamWriter strWriter;
FileStream fErrorStream;
StreamWriter strErrorWriter;
try
{
fStream = new FileStream("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
strWriter = new StreamWriter(fStream);
}
catch (Exception e)
{
Console.WriteLine("Cannot open Redirect.txt for writing");
Console.WriteLine(e.Message);
return;
}
try
{
fErrorStream = new FileStream("./RedirectError.txt", FileMode.OpenOrCreate, FileAccess.Write);
strErrorWriter = new StreamWriter(fErrorStream);
}
catch (Exception e)
{
Console.WriteLine("Cannot open RedirectError.txt for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(strWriter);
Console.SetError(strErrorWriter);
Console.WriteLine("Number of command line parameters = {0}", args.Length);
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
// Store parameters into variables for reference
string pSuccess = "OK";
string pFail = "FAIL";
string pOutputFile = args[0];
string pLookupType = args[1];
string pUserName = args[2];
string pPassword = args[3];
Console.SetOut(defaultOutputMethod);
// Console.SetError(defaultErrorOutputMethod);
strWriter.Close();
fStream.Close();
// Setup Commnet filestream and stream writer, and assign output to stream if file successfully created
FileStream fCommnetStream;
StreamWriter strCommnetWriter;
string sCommnetOutputFile = @"./CP07.txt";
try
{
fCommnetStream = new FileStream(sCommnetOutputFile, FileMode.OpenOrCreate, FileAccess.Write);
strCommnetWriter = new StreamWriter(fCommnetStream);
}
catch (Exception e)
{
Console.WriteLine("Cannot open " + sCommnetOutputFile + " for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(strCommnetWriter);
// Test Variables to determine output: Success or Failure
string sSuccessPass = "1111";
string sFailPass = "0000";
if (pPassword == sSuccessPass)
{
Console.WriteLine(pSuccess);
}
else if (pPassword == sFailPass)
{
Console.WriteLine(pFail);
}
else
{
Console.WriteLine("OTHER");
}
Console.WriteLine("Output File: <" + pOutputFile + ">");
Console.WriteLine("Lookup Type: <" + pLookupType + ">");
Console.WriteLine("User Name: <" + pUserName + ">");
Console.WriteLine("User Pass: <" + pPassword + ">");
Console.SetOut(defaultOutputMethod);
Console.SetError(defaultErrorOutputMethod);
strCommnetWriter.Close();
fCommnetStream.Close();
strErrorWriter.Close();
fErrorStream.Close();
Console.Write("Press any key to finish: ");
Console.Write("Ending...", Console.ReadLine());
//Console.WriteLine("Done");
}
}
}
I guess the problem is that you are writing into the “current directory”, which can be anything when your program is started by a batch file, which itself has been started by another application. It is not impossible that your files are written into the directory where the batch file resides 🙂