在程序文件中查找文件并启动该文件

时间:2012-05-06 23:55:08

标签: c#

我正在尝试编写一个下载Team Viewer并安装它的程序,如果已安装它,请从程序文件中启动teamviewer,而不是重新下载它。

我有它所以它下载并安装teamviewer到正确的文件夹,但我不知道如何告诉我的程序在64位程序文件(x86)中搜索或32位程序文件搜索目录和子目录teamviewer.exe并启动该程序。这是我到目前为止的代码。

感谢。

我使用此代码解决了它。它在正确的程序文件文件夹中搜索Teamviewer并启动该程序。这不会检查你是否安装了它,我在我的程序中检查过它,但它很容易添加。

private void teamviewerbtn_Click(object sender, EventArgs e)
    {
        //start button

        if (Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").Contains("64"))
        {
            string path = @"c:\Program Files (x86)\Teamviewer\"; //specify starting folder location for searching
            string searchPattern = "teamviewer.exe*"; //what do you want to search for?

            DirectoryInfo di = new DirectoryInfo(path);

            FileInfo[] files =
                di.GetFiles(searchPattern, SearchOption.AllDirectories);

            foreach (FileInfo file in files)
            {
                string tvE = (file.FullName.ToString()); //takes found file and references full file path
                Process.Start(tvE);
            }
        }
        else
        {
            string path = @"c:\Program Files\Teamviewer\"; //specify starting folder location for searching
            string searchPattern = "teamviewer.exe*"; //what do you want to search for?

            DirectoryInfo di = new DirectoryInfo(path);

            FileInfo[] files =
                di.GetFiles(searchPattern, SearchOption.AllDirectories);

            foreach (FileInfo file in files)
            {
                string tvE = (file.FullName.ToString()); //takes found file and references full file path
                Process.Start(tvE);
            }
        }   

        //end button
    }

1 个答案:

答案 0 :(得分:0)

if (files.Length == 1)
{
    Process.Start(files[0]);
}