2009-11-15 69 views
1

我需要做一个检查,看看文件是否存在,他们的输入,我怎么能做到这一点,我尝试使用尝试&渔获物和它没有任何效果如何捕捉异常并继续程序? C#

if (startarg.Contains("-del") == true) 
      { 
       //Searches "Uninstallers" folder for uninstaller containing the name that they type after "-del" and runs it 
       string uninstalldirectory = Path.Combine(Directory.GetCurrentDirectory(), "Uninstallers"); 
       DirectoryInfo UninstallDir = new DirectoryInfo(uninstalldirectory); 
       string installname = startarg[2].ToString(); 
       //Removes file extesion "-del " 
       installname.Remove(0, 5); 
       string FullFilePath = Path.Combine(uninstalldirectory, installname); 
       try 
       { 
        //Makes the uninstaller invisible to the user and sets other settings 
        Process Uninstaller = new Process(); 
        Uninstaller.StartInfo.FileName = FullFilePath; 
        Uninstaller.StartInfo.UseShellExecute = false; 
        Uninstaller.StartInfo.CreateNoWindow = true; 
        Uninstaller.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
        Uninstaller.Start(); 
       } 
       //Only is run if the package isn't installed 
       catch (System.Exception) 
       { 
        Console.WriteLine("The specified package is not installed, you most likely mispelled it or didnt put quotes around it, try again"); 
       } 

      } 

该代码的绝大部分是获得当前目录并添加“卸载程序”。

编辑: 调试结果是ArgumentOutOfRangeException

我使用File.Exists if语句和else尝试,它仍然崩溃

编辑#2:什么

只是有点我与这个程序有关:我试图编写一个跨平台(使用mono,还没有移植它,因为我不喜欢MonoDevelop)包管理器,这是它的功能,它删除包。它通过在应用程序的Uninstallers文件夹中获取卸载脚本来获取已安装应用程序的列表。我希望它是独立的目录,所以我必须得到它的当前目录

我的代码工作正常,如果该文件存在,但是当它不崩溃这就是我的问题

+2

当你运行这个命令时,你看到了什么结果,指定一个不存在的文件? “它没有效果”是什么意思? – 2009-11-15 23:58:36

+0

@lndebi,将你的评论拼凑在一起,你会在'string FullFilePath ...'行得到'ArgumentOutOfRange'异常。查看我答案中的第一个项目符号,以解决您的紧急问题。 – 2009-11-16 00:47:27

+0

...和詹姆斯的答案直接解决它。 – 2009-11-16 00:50:27

回答

3

try-catch没有效果,因为异常被try块外的代码抛出。正如其他人指出的那样,您可以对代码进行一些改进,以便在真正异常的情况下调用异常处理。

3

这是不好的初步实践依赖关于正常处理的例外情况。您可以使用File.Exists() function来检查文件是否存在,以及它是否不写入警报并允许它们选择另一个文件。所以它可能看起来像

if(File.Exists(FullFilePath)) 
{ 
    //uninstall 
} 
else 
{ 
    Console.WriteLine("The specified package is not installed, you most likely mispelled it or didnt put quotes around it, try again"); 
} 
+0

我改变后,它仍然崩溃 – 2009-11-16 00:09:22

+0

@ lndebi:崩溃在哪里?在什么情况下,什么是例外? – 2009-11-16 00:11:21

+0

它崩溃在 字符串FullFilePath = Path.Combine(uninstalldirectory,installname); – 2009-11-16 00:12:44

6

你还没有指定你看到的结果,所以你的问题很难诊断。我可以看到一些可能出现的问题,虽然:

  • Path.Combine可以抛出异常 如果它的参数包含路径无效字符 。您还没有 在 包裹您的Path.Combine调用try-catch块。
  • 如果你的代码需要在给定路径的文件或目录 存在, 你最好检查与 一个 File.ExistsDirectory.Exists 调用,而不是依赖于一个 例外。 Joel Coehoorn在他的评论中提到了一个很好的观点,就使用File.Exists时的竞争条件而言。
  • 从您的 命令行参数中剥离“-del”是一种相当容易出错的方式来处理参数。 是否有任何理由,你不能简单地 期望指令(“-del”)为 第一个参数,并且 的路径是第二个参数?

编辑:其他地方阅读您的答复后,我看到了另一个问题:

//Removes file extesion "-del " 
installname.Remove(0, 5); 

这不会做你认为它。你需要该行的结果分配回installName

installname = installname.Remove(0, 5); 

我也很担心,你期待一个指令和路径以某种方式结合到你的第三个命令行参数。如果调用应用程序,像这样:

myapp.exe foo bar -del "C:\myfile.txt" 

,您的命令行参数看起来像下面这样:

args[0] // foo 
args[1] // bar 
args[2] // -del 
args[3] // C:\myfile.txt 

换句话说,“-del”和你的文件路径将会在单独参数。

+0

我不想让它执行另一个程序,我正在编写一个跨平台的软件包管理器(很快将它移植到单声道),这是删除已安装软件包的一部分。我希望用户能够输入simtho -del“Mozilla Firefox”,它会删除Mozilla Firefox。不要担心它的很多细节,我主要介绍了它,只是请帮助我解决这个问题 – 2009-11-16 00:32:39

+1

坦率地说,lndebi,你的代码中充满了错误,你需要详细说明它的细节,我会以任何合理的方式工作。有没有任何理由让我回答我所问的调试问题? – 2009-11-16 00:35:28

+0

您的第二个项目符号错误,因为文件系统是_volatile_,这意味着您可以删除文件或在您检查和尝试使用该文件时权限发生变化。无论如何,您必须能够处理异常,因此.Exists()调用只是额外的。 – 2009-11-16 00:55:21