通过vb.net运行bat文件时出错 [英] error in running bat file though vb.net

查看:120
本文介绍了通过vb.net运行bat文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在创建并从vb.net调用批处理文件来运行java程序(bat文件中的java命令)。



bat文件创建正常......但是在运行它时,java命令前面会出现一些奇怪的符号,它阻止系统将java识别为任何类型命令。

我想知道那是什么东西!!!



这里使用的代码:



 进口系统
进口 System.IO

模块 Module1

Sub Main()

Dim str,s1 As String

s1 = wednesday
str = java pname& s1&

File.Create( D:\\ \\ _ \\\ MSG \sms_old\send.bat)。Dispose()

My.Computer.FileSystem.WriteAllText( D:\\\\ MSG \sms_old\send.bat,str.Trim(),错误


Dim processinfo As ProcessStartInfo()
processinfo.WorkingDirectory = D:\\\\ MSG \sms_old
processinfo.FileName = send.bat
Process.Start(processinfo)

结束 Sub

结束 模块





这应该在命令提示符下生成:



D:\\\\ MSG \sms_old> java pname周三



但是它正在产生这样的东西:



D:\\\\ MSG \sms_old>javapnamewednesday



任何人都可以点亮它吗?



非常感谢!!!

解决方案

我看不出任何错误 - 除了文件创建代码,这是不必要的而且很奇怪。

我这样做:

  Dim  str,s1  As  < span class =code-keyword> String  

s1 = w星期三
str = java pname& s1&
File.WriteAllText( D:\ sa \ MSG\sms_old\send.bat,str.Trim())

Dim processinfo As ProcessStartInfo()
processinfo.WorkingDirectory = D:\\\\MSG \sms_old
processinfo.FileName = send.bat
Process.Start(processinfo)



甚至使用java作为进程和余数作为ProcessInfo对象的参数 - 完全忽略BAT文件。


这些奇怪的字符恰好是3个字符:它们提供有关多字节编码的信息e编码的文本文件。

所以看起来ProcessStartInfo需要一个ASCII(或其他8位)编码文件。

System.IO.File.WriteAllText有overlayod接受编码参数 -

 System.IO.File.WriteAllText(  D:\\\\ MSG \sms_old\send.bat,str.Trim(),System.Text.Encoding.ASCII)



但是当然,正如OriginalGriff所提到的,你不需要bat文件,只需正确使用Process / ProcessStartInfo的命令和参数

 System.Diagnostics。 Process.Start(  java  pname& s1&     


Hi Everyone,

I am creating and calling a batch file from vb.net to run a java program(java command inside bat file).

The bat file is creating fine....but while running it some kind of weird sign is coming in front of the java command that is stopping system from recognizing java as any type of command.
I wonder what could be that thing!!!

here's code I used:

Imports System
Imports System.IO

Module Module1

    Sub Main()

        Dim str, s1 As String

        s1 = "wednesday"
        str = "java pname """ & s1 & """"

        File.Create("D:\sa\MSG\sms_old\send.bat").Dispose()

        My.Computer.FileSystem.WriteAllText("D:\sa\MSG\sms_old\send.bat", str.Trim(), False)


        Dim processinfo As New ProcessStartInfo()
        processinfo.WorkingDirectory = "D:\sa\MSG\sms_old"
        processinfo.FileName = "send.bat"
        Process.Start(processinfo)

    End Sub

End Module



This should have produced this in command prompt:

D:\sa\MSG\sms_old>java pname "wednesday"

But it is producing something like this:

D:\sa\MSG\sms_old>java pname "wednesday"

Can anyone throw any light on it?

Thanks a lot!!!

解决方案

I can't see anything too wrong - except the file creation code which is unnecessary and rather odd.
I'd do it like this:

Dim str, s1 As String

s1 = "wednesday"
str = "java pname """ & s1 & """"
File.WriteAllText("D:\sa\MSG\sms_old\send.bat", str.Trim())

Dim processinfo As New ProcessStartInfo()
processinfo.WorkingDirectory = "D:\sa\MSG\sms_old"
processinfo.FileName = "send.bat"
Process.Start(processinfo)


Or even use "java" as the process and the remainder as the arguments to the ProcessInfo object - ignoring a BAT file completely.


Those weird characters are exactly 3 characters: they provide information on the encoding used in multi-byte encoded text files.
So it looks like ProcessStartInfo expects an ASCII (or other 8bit) encoded file.
System.IO.File.WriteAllText has overlaod which accepts an encoding parameter -

System.IO.File.WriteAllText("D:\sa\MSG\sms_old\send.bat", str.Trim(), System.Text.Encoding.ASCII)


But of course, as OriginalGriff mentioned, you do not need the bat file, just use the command and parameters of the Process / ProcessStartInfo correctly

System.Diagnostics.Process.Start("java", "pname """ & s1 & """)


这篇关于通过vb.net运行bat文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆