我需要做一个检查,看看文件是否存在,他们的输入,我怎么能做到这一点,我尝试使用尝试&渔获物和它没有任何效果如何捕捉异常并继续程序? 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文件夹中获取卸载脚本来获取已安装应用程序的列表。我希望它是独立的目录,所以我必须得到它的当前目录
我的代码工作正常,如果该文件存在,但是当它不崩溃这就是我的问题
当你运行这个命令时,你看到了什么结果,指定一个不存在的文件? “它没有效果”是什么意思? – 2009-11-15 23:58:36
@lndebi,将你的评论拼凑在一起,你会在'string FullFilePath ...'行得到'ArgumentOutOfRange'异常。查看我答案中的第一个项目符号,以解决您的紧急问题。 – 2009-11-16 00:47:27
...和詹姆斯的答案直接解决它。 – 2009-11-16 00:50:27