VB在子文件夹中执行.bat以重命名文件

时间:2016-08-27 12:45:14

标签: vb.net shell batch-file directory

我想在VB中执行一个子文件夹中的.bat来重命名一些文件。该文件夹是dinamyc,子文件夹的名称是静态的。

     path = System.IO.Path.GetDirectoryName( _
     System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
     Dim psi As New ProcessStartInfo(path & "\subfolder\kappa.bat")

这会在我启动时产生错误*而且这不会做任何事情

 Shell(CreateObject("Wscript.Shell").CurrentDirectory & "\subfolder\" & "kappa.bat", AppWinStyle.Hide)
当我尝试

时,path变量显示超出了我的需要
MessageBox.Show(path)

http://image.prntscr.com/image/e0398d6e69ff48ce80fd826cced03612.png

所以

path

不是我需要的,但我找不到与我的问题有关的任何内容

编辑:还试过这个

 Dim psi As New ProcessStartInfo(path & "\subfolder\kappa.bat")
        psi.RedirectStandardError = True
        psi.RedirectStandardOutput = True
        psi.CreateNoWindow = False
        psi.WindowStyle = ProcessWindowStyle.Hidden
        psi.UseShellExecute = False
         Process.Start(psi)

path & "\subfolder\kappa.bat将生成执行中的偏移量<误差

The `GetExecutingAssembly` error is :

System.IO.FileNotFoundException: File not found.
   at Microsoft.VisualBasic.Interaction.Shell(String PathName, AppWinStyle Style, Boolean Wait, Int32 Timeout)
   at Patcher.Form1.BTN_Click(Object sender, EventArgs e) in D:\Patcher\Form1.vb:line 58
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1080.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Patcher
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.0.0
    CodeBase: file:///D:/Patcher.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 14.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1081.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1078.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1064.2 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

1 个答案:

答案 0 :(得分:1)

您的问题出在System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase行。该方法返回以file:\开头的路径,当您从路径中删除文件名时,它不再是您尝试访问的文件,这就是它无法正常工作的原因。

有几种方法可以获取应用程序的文件夹。两个示例是Application.StartupPathSystem.AppDomain.CurrentDomain.BaseDirectory。就个人而言,我最喜欢后者,不要问我为什么。

因此,为了解决您的问题,您应该只需要这样做:

path = System.AppDomain.CurrentDomain.BaseDirectory
相关问题