Cannot start batch process with C#

Started by
4 comments, last by musafir2007 11 years, 10 months ago
Hi, I write a simple C# code to create a batch file and then save that file in C:/file.bat.
At the end of my c# program I launch it, and I get following error. This only happens on Windows 7, code works just as expected on windows xp! thanks

CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.



using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamWriter file = new System.IO.StreamWriter("c:\\file123.bat");
file.WriteLine("ipconfig /all");
file.WriteLine("pause");
file.Close();
Process.Start("c:\\file123.bat");
Thread.Sleep(500);
File.Delete("c:\\file123.bat");
}
}
}
Advertisement
This only happens on Windows 7, code works just as expected on windows xp!


This is because the user does not have permission to write the root directory (see: http://technet.microsoft.com/en-us/library/bb727008.aspx). Also, you should consider creating the file in the user's temporary directory.

Or, if this is the only command in the batch, consider redirecting the standard output of the Process via ProcessStartInfo.RedirectStandardOutput and Process.StandardOutput) and invoke "ipconfig.exe" directly.
thanks for reply. But ipconfig was just an example and the batch file does more.
Copying it to temp directory did not make a difference, also copying is not an issue. Even if I manually run the file from c: I get the same error.
Basically the code gets the ip from ipconfig /all and then using that ip to do "telnet xxx.xxx.xxx.xxx".
Again this works fine on xp, so i am guessing there is something specific I need to change for windows 7.
thanks
Have you tried running your application as administrator?

  1. Don't ever write to C:\:
    The C:\ root directory is a protected directory and thus requires administrative level permissions to create, edit, or delete files from on many systems. If you need to store a temporary file somewhere, stick it in a temp file created using the appropriate APIs: System.IO.Path.GetTempFileName(). This applies for Windows NT and beyond, which includes Windows 2000, XP, Vista, 7, and 8.
  2. You do not need to use a batch file to do that at all. .Net already includes libraries for enumerating IP addresses, and executing telnet does not require the use of a batch file.
  3. Lastly, the error typically indicates that you are attempting to use a UNC path, which are not properly supported by CMD (For historical reasons), as such it tend defaults the current working directory to %WINDIR%

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

can you please explain more on "

[background=rgb(250, 251, 252)]and executing telnet does not require the use of a batch file." [/background]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]what other alternative do I have. I basically want to avoid, ipconfig and copying ip for cmd: telnet xxx.xxx.xxx.xxx. Since the ip changes, i wanted to simply automate ipconfig each time.[/background][/font]



btw, I have also tried, but got the same error on windows 7 (and NOT on xp)


string deviceIP = getDeviceIP();
Process.Start("cmd.exe", "/C telnet " + deviceIP);


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]thanks[/background][/font]

This topic is closed to new replies.

Advertisement