简体   繁体   English

如何使用 c# 运行 exe 文件(我不能使用 Process.Start() 因为我不知道 exe 文件的位置)

[英]How can I run an exe file with c# (I can't use Process.Start() because I don't know the exe file's location)

I want to run an exe file with c# but I can't use Process.Start() because I don't know the exe file's location.我想用 c# 运行一个 exe 文件,但我不能使用Process.Start()因为我不知道 exe 文件的位置。

I didn't start to writing so I don't have any code for now.我还没有开始写,所以我现在没有任何代码。

Use following:使用以下:

            string locateFile = "cmd.exe";
            string environPath = Environment.GetEnvironmentVariable("Path");
            string[] paths = environPath.Split(new char[] { ';' }).ToArray();

            string filePath = "";
            foreach (string path in paths)
            {
                string file = Directory.GetFiles(path, locateFile, SearchOption.TopDirectoryOnly).FirstOrDefault();
                if (file != null)
                {
                    filePath = file;
                    break;
                }
            }
            if (filePath.Length > 0)
            {
                Console.WriteLine("File location : '{0}'", filePath);
            }
            else
            {
                Console.WriteLine("File not found");
            }
            Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM