4

I have this code piece that used in my c# form project. I also have exp.bat file contains shell commands as below. But whatever I do, it does not create .txt file at working directory.

@echo off
echo "hello" > test.txt
path = @"‪C:\Users\abc\Desktop\exp.bat";
startingPath = @"C:\Users\abc\Desktop\";
bool success = false;
try
{
    System.Diagnostics.ProcessStartInfo ProcStartInfo = new 
    System.Diagnostics.ProcessStartInfo("cmd");
    ProcStartInfo.RedirectStandardOutput = true;
    ProcStartInfo.UseShellExecute = false;
    ProcStartInfo.CreateNoWindow = false;
    ProcStartInfo.RedirectStandardError = true;
    System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
    ProcStartInfo.Arguments = "/c start /wait"+path;
    ProcStartInfo.WorkingDirectory = startingPath;
    MyProcess.StartInfo = ProcStartInfo;
    success = MyProcess.Start();
    MyProcess.WaitForExit();
}
catch (Exception ex) { string s = ex.StackTrace.ToString();}
Skandix
  • 1,916
  • 6
  • 27
  • 36
  • That way you're handling the error shows you nothing. You may be experiencing and error (i am assuming "Access Denied") and you don't receive anything. Just for the hell of it, try running VS as Administrator. – Renato Afonso Feb 20 '18 at 08:32
  • @Renato, I tried it as Administrator, but nothing changed. –  Feb 20 '18 at 09:34
  • @Mofi, I have no chance, because I can't change, arrange or anything in the real bat file. –  Feb 20 '18 at 09:38
  • Please change that catch to at least send that s string to Console.Writeline to check if you have an error – Renato Afonso Feb 20 '18 at 09:48
  • I did what you said and now I can run my bat file in Windows 8. But when I transfer program to Windows 10, I get this error as "Windows can not find "C:\Users\abc\Desktop\exp.bat". Make sure you typed the name correctly, and try again." I really do not know where to write this update. So I wrote it in the comments section. –  Feb 22 '18 at 08:39
  • 1
    Use `%USERPROFILE%\Desktop\exp.bat` instead of `C:\Users\abc\Desktop\exp.bat` to reference the batch file `exp.bat` in desktop folder of current user account independent on name of user account and storage location. See Wikipedia article about [Windows Environment Variables](https://en.wikipedia.org/wiki/Environment_variable#Windows) for a list of predefined environment variables with description. And please take also a look on [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/a/41461002/3074564) – Mofi Feb 22 '18 at 12:22
  • 1
    In C# code use the method [Environment.GetEnvironmentVariable](https://msdn.microsoft.com/en-us/library/77zkk0b6.aspx) to get the string value of predefined Windows environment variable `USERPROFILE` to build the paths for `exp.bat` and starting directory dynamically already within C# application. Or even better get current user desktop folder directly, see [How to get a path to the desktop for current user in C#?](https://stackoverflow.com/questions/634142/) – Mofi Feb 22 '18 at 12:25
  • Thanks @Mofi. you solved it. Thanks a lot. –  Feb 22 '18 at 14:25
  • Mofi can you please post an answer so this doesn't show up as unanswered? –  Jun 08 '18 at 14:16

2 Answers2

0

Originally posted by use Mofi in comments.

posting the same answer as is so this question is not counted in unanswered, question author also confirms in comments that the answer by Mofi was correct and it helped.

I think enough background, here is the comment as the answer.

In C# code use the method Environment.GetEnvironmentVariable to get the string value of predefined Windows environment variable USERPROFILE to build the paths for exp.bat and starting directory dynamically already within C# application. Or even better get current user desktop folder directly, see How to get a path to the desktop for the current user in C#?Mofi Feb 22 at 12:25

Baljeetsingh Sucharia
  • 1,990
  • 1
  • 19
  • 37
0

You can easily achieve that by adding the following commands to the beginning of your bat file.

 %~d0
 cd %~dp0
Amir Pourmand
  • 519
  • 6
  • 17