I'm having a strange problem when running a bat file from c# code(Also from cmd.exe and Run window). First of all, my bat file looks like this
file_copy.bat
NET USE %1 /USER:%2 %3
COPY /Y %4 %5
NET USE %1 /DELETE
PAUSE
I've included this bat file in visual studio 2013, and set Copy To Output Directory
to Always Copy
from properties. My code to run this bat file looks like this
string args = "\"\\\\192.168.54.196\\c$\" \"user\" \"123456\" \"\\\\192.168.54.196\\c$\\paykiosk\\logs\\pgw_150113.log\" \"C:\\Users\\IlhamIs\\AppData\\Local\\Temp\\pgw_150113.log\""
var processInfo = new ProcessStartInfo
{
UseShellExecute = false,
FileName = "file_copy.bat",
Arguments = args
};
var process = new Process { StartInfo = processInfo };
process.Start();
I build my project and obviously file_copy.bat is copied to bin\debug
directory(in my case). Then I run application, bat file executes, but first command is somehow modified at runtume, therefore an error occurs saying 'я╗┐NET' is not recognized as an internal or external command, operable program or batch file.
. This is wierd, я╗┐
is written to the beginning of first command. Here is complete output that I got:
C:\Users\IlhamIs\Desktop\test\bin\debug>я╗┐NET USE "\\192.168.54.196\c$" /USER:"user" "123456"
'я╗┐NET' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\IlhamIs\Desktop\test\bin\debug>COPY /Y "\\192.168.54.196\c$\paykiosk\logs\pgw_150113.l
og" "C:\Users\IlhamIs\AppData\Local\Temp\pgw_150113.log"
Logon failure: unknown user name or bad password.
C:\Users\IlhamIs\Desktop\test\bin\debug>NET USE "\\192.168.54.196\c$" /DELETE
The network connection could not be found.
More help is available by typing NET HELPMSG 2250.
C:\Users\IlhamIs\Desktop\test\bin\debug>PAUSE
Press any key to continue . . .
Then create a bat file in bin\debug
directory with the same script (file_copy_2.bat), note that this is not created by visual studio. This time I change FileName = "file_copy.bat"
to FileName = "file_copy_2.bat"
to make my application to run the file I created and then I run my application. It looks ridiculous, but it worked, all 3 commands executed successfully.
This is really strange. What am I missing? Does visual studio do something special with bat file during compilation and copying file to output directory? Both file_copy.bat
and file_copy_2.bat
are the same (I've checked attributes just in case, also the same). The file visual studio created fails to execute first command, BUT the one I created myself succeeds. Why?
FYI,
Code Page code returned from command chcp is 866. I've tried 65001 which is utf-8, but I've got similar result, different characters written to the beginning of command instead of я╗┐
Please comment below if question details aren't clear to you, as I could explain wrong
Thanks in advance