Sign in to follow this  
musafir2007

Cannot start batch process with C#

Recommended Posts

musafir2007    201
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.

[code]

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");
}
}
}
[/code]

Share this post


Link to post
Share on other sites
fastcall22    10838
[quote name='musafir2007' timestamp='1339599717' post='4948827']This only happens on Windows 7, code works just as expected on windows xp![/quote]

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

Or, if this is the only command in the batch, consider redirecting the standard output of the Process via [url=http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx]ProcessStartInfo.RedirectStandardOutput[/url] and [url=http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx]Process.StandardOutput[/url]) and invoke "ipconfig.exe" directly. Edited by fastcall22

Share this post


Link to post
Share on other sites
musafir2007    201
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

Share this post


Link to post
Share on other sites
Washu    7829
[list=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: [url="http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename.aspx"]System.IO.Path.GetTempFileName()[/url]. This applies for Windows NT and beyond, which includes Windows 2000, XP, Vista, 7, and 8.
[*]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.
[*]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%
[/list] Edited by Washu

Share this post


Link to post
Share on other sites
musafir2007    201
can you please explain more on "
[left][background=rgb(250, 251, 252)]and executing telnet does not require the use of a batch file." [/background][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][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][/size][/font][/color][/left]

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

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

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]thanks[/background][/size][/font][/color][/left] Edited by musafir2007

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this