本文介绍了如何在c#中的给定位置打开文件资源管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为变量中的给定目录位置创建代码,例如:

string _path =c:/ windows



按钮点击事件在该路径上启动文件浏览器?



我正在询问cmd提示中启动命令的确切方式,例如在cmd中:startstart c :/ windows,任何想法?



希望你明白我的意思

how to make code that for given directory location in variable for example:
string _path = "c:/windows"

on button click event to start file explorer at that path?

im asking something exactly how start command is working in cmd prompt for example in cmd is: start "start c:/windows", any ideas?

hope you understand what i mean

推荐答案

// required in addition to other 'using necessary
using System.Diagnostics;
using System.IO;

private void OpenFolder(string folderPath)
{
    if (Directory.Exists(folderPath))
    {
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            Arguments = folderPath,
            FileName = "explorer.exe";
        };

        Process.Start(startInfo);
    }
    else
    {
        MessageBox.Show(string.Format("{0} Directory does not exist!", folderPath));
    }
}

注意:如果我在生产代码中写这个,我可能会将整个事情包装在Try / Catch中以拦截可能的访问权限错误。

Note: if I was writing this in production code, I'd probably wrap the whole thing in a Try/Catch to intercept possible access permission errors.


System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
string _path = "c:/windows";
startInfo.Arguments = string.Format("/C start {0}", _path);
process.StartInfo = startInfo;
process.Start();



这篇关于如何在c#中的给定位置打开文件资源管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 09:14