运行BATCH文件问题,请有人帮我 [英] Running a BATCH file PROBLEM, please somebody help me

查看:71
本文介绍了运行BATCH文件问题,请有人帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用三种不同的方法,所有方法都可以在我的其他项目中使用,我当前的项目是Windows服务,我正在通过类调用这些方法.

当我调试它的时候,我对任何一个都没有异常!

I have tried fallowing three different methods, all methods works in my other projects, my current project is a windows service, I''m calling the methods through my class.

When I debug it out I''m not getting exception at any of them !

Try
    Try
        Directory.SetCurrentDirectory("C:\")
        Dim p As New Process
        Dim pi As New ProcessStartInfo
        pi.Verb = "print"
        pi.FileName = FilePATH
        pi.UseShellExecute = True
        p.StartInfo = pi
        p.Start()
    Catch ex As Exception
    End Try
    Try
        System.Diagnostics.Process.Start(FilePATH)
    Catch ex As Exception
    End Try
    Try
        Shell(FilePATH, AppWinStyle.NormalFocus)
    Catch ex As Exception
    End Try
Catch ex As Exception
    MsgBox(ex.Message)
    EventLog.WriteEntry("PEGASUS", "ExecuteFile___" & DateTime.Now & ">>>>" & ex.Message, EventLogEntryType.Warning)
End Try



[edit]已添加代码块,请勿大声喊叫.使用所有大写字母被认为是在互联网上大喊大叫,并且粗鲁(使用所有小写字母被认为是幼稚的).如果您想受到重视,请使用适当的大写字母-OrignalGriff [/edit]

这里没有C#.删除了错误的标签. Toli Cuturicu



[edit]Code block added, DON''T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously - OrignalGriff[/edit]

No C# here. Removed bad tag. Toli Cuturicu

推荐答案

很难说是怎么回事,因为您吞没了所有异常.要么删除内部的try块,然后只保留外部的try块,要么填充内部的catch块并报告错误.

照原样,任何错误都将被报告捕获块吞没,并且永远不会看到.
It''s difficult to tell what is going on, because you are swallowing all exceptions. Either remove your inner try blocks and just leave the outer one, or populate your inner catch blocks and report errors.

As it is, any error will be swallowed and never seen by the reporting catch block.


首先,您需要检查批处理文件路径并在您的应用程序中正确访问此路径. br/> 即使将文件放在可执行目录中,也永远不要假定它位于工作目录中.工作目录完全取决于您在何处启动可执行文件.

假设您将文件放在可执行目录中.更确切地说,这是入口组合件的主要模块"的目录.这是找出路径的方法:

First of all, you need to check batch file path and access this path properly in your application.
You should never assume it is in your working directory, even if you put your file in your executable directory. The working directory totally depends on where your start your executable.

Let''s assume you put your file in your executable directory. More exactly, this is the directory of the "main module of entry assembly". This is how to find out its path:

exePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);



顺便说一句,格里夫(Griff)对异常是正确的:您不应该像您那样阻止异常传播!
看到这个:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理此问题? [



By the way, Griff is right on exceptions: you should not block exception propagation like you do!
See this:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^]
This is also important if you ask a CodeProject Question.

—SA


这篇关于运行BATCH文件问题,请有人帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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