C# 使用 Process.Start 打开文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1132422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a folder using Process.Start
提问by Daniel
I saw the other topicand I'm having another problem. The process is starting (saw at task manager) but the folder is not opening on my screen. What's wrong?
我看到了另一个主题,但我遇到了另一个问题。该过程正在启动(在任务管理器中看到),但该文件夹未在我的屏幕上打开。怎么了?
System.Diagnostics.Process.Start("explorer.exe", @"c:\teste");
采纳答案by Fredrik M?rk
Have you made sure that the folder "c:\teste
" exists? If it doesn't, explorer will open showing some default folder (in my case "C:\Users\[user name]\Documents
").
您确定文件夹“ c:\teste
”存在吗?如果没有,资源管理器将打开显示一些默认文件夹(在我的情况下为“ C:\Users\[user name]\Documents
”)。
Update
更新
I have tried the following variations:
我尝试了以下变体:
// opens the folder in explorer
Process.Start(@"c:\temp");
// opens the folder in explorer
Process.Start("explorer.exe", @"c:\temp");
// throws exception
Process.Start(@"c:\does_not_exist");
// opens explorer, showing some other folder)
Process.Start("explorer.exe", @"c:\does_not_exist");
If none of these (well, except the one that throws an exception) work on your computer, I don't think that the problem lies in the code, but in the environment. If that is the case, I would try one (or both) of the following:
如果这些(好吧,除了抛出异常的那个)在您的计算机上都不起作用,我认为问题不在于代码,而在于环境。如果是这种情况,我会尝试以下一项(或两项):
- Open the Run dialog, enter "explorer.exe" and hit enter
- Open a command prompt, type "explorer.exe" and hit enter
- 打开运行对话框,输入“explorer.exe”并回车
- 打开命令提示符,输入“explorer.exe”并回车
回答by Kevin Laity
You're using the @ symbol, which removes the need for escaping your backslashes.
您正在使用 @ 符号,这消除了转义反斜杠的需要。
Remove the @ or replace \\ with \
删除 @ 或用 \ 替换 \\
回答by Joel Coehoorn
You don't need the double backslash when using unescaped strings:
使用未转义的字符串时不需要双反斜杠:
System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
回答by Austin Salonen
You're escaping the backslash when the at sign does that for you.
当 at 符号为您执行此操作时,您正在逃避反斜杠。
System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
回答by Jeremy Cron
Use an overloaded version of the method that takes a ProcessStartInfo instance and set the ProcessWindowStyle property to a value that works for you.
使用采用 ProcessStartInfo 实例并将 ProcessWindowStyle 属性设置为适合您的值的方法的重载版本。
回答by sgmoore
Strange.
奇怪的。
If it can't find explorer.exe, you should get an exception. If it can't find the folder, it should still open some folder (eg my Documents)
如果它找不到 explorer.exe,你应该得到一个异常。如果找不到文件夹,它仍然应该打开某个文件夹(例如我的文档)
You say another copy of Explorer appears in the taskmanager, but you can't see it.
你说任务管理器中出现了另一个 Explorer 副本,但你看不到它。
Is it possible that it is opening offscreen (ie another monitor)?
它是否有可能在屏幕外打开(即另一台显示器)?
Or are you by any chance doing this in a non-interactive service?
或者您是否有机会在非交互式服务中执行此操作?
回答by OregonGhost
Just for completeness, if all you want to do is to open a folder, use this:
为了完整起见,如果您只想打开一个文件夹,请使用以下命令:
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() {
FileName = "C:\teste\",
UseShellExecute = true,
Verb = "open"
});
Ensure FileName ends with Path.DirectorySeparatorChar
to make it unambiguously point to a folder. (Thanks to @binki.)
确保 FileName 以 结尾,Path.DirectorySeparatorChar
使其明确指向文件夹。(感谢@binki。)
This solution won't work for opening a folder and selecting an item, since there doesn't seem a verb for that.
此解决方案不适用于打开文件夹和选择项目,因为似乎没有动词。
回答by schar
Does it open correctly when you run "explorer.exe c:\teste" from your start menu? How long have you been trying this? I see a similar behavior when my machine has a lot of processes and when I open a new process(sets say IE)..it starts in the task manager but does not show up in the front end. Have you tried a restart?
当您从开始菜单运行“explorer.exe c:\teste”时,它是否正确打开?你尝试这个多久了?当我的机器有很多进程并且当我打开一个新进程(设置为 IE)时,我看到类似的行为。它在任务管理器中启动,但没有显示在前端。你试过重启吗?
The following code should open a new explorer instance
下面的代码应该打开一个新的资源管理器实例
class sample{
static void Main()
{
System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
}
}
回答by Curtis
Do you have a lot of applications running when you are trying this? I encounter weird behavior at work sometimes because my system runs out of GDI Handles as I have so many windows open (our apps use alot).
当你尝试这个时,你有很多应用程序在运行吗?有时我会在工作中遇到奇怪的行为,因为我的系统用完了 GDI 句柄,因为我打开了很多窗口(我们的应用程序使用了很多)。
When this happens, windows and context menus no long appear until I close something to free up some GDI handles.
发生这种情况时,窗口和上下文菜单不会再出现,直到我关闭某些东西以释放一些 GDI 句柄。
The default limit in XP and Vista is 10000. It is not uncommon for my DevStudio to have 1500 GDI handles, so if you have a couple of copies of Dev studio open, it can eat them up pretty quickly. You can add a column in TaskManager to see how many handles are being used by each process.
XP 和 Vista 中的默认限制是 10000。我的 DevStudio 拥有 1500 个 GDI 句柄的情况并不少见,因此如果您打开了几个 Dev Studio 副本,它可以很快吃掉它们。您可以在 TaskManager 中添加一列以查看每个进程正在使用多少句柄。
There is a registry tweak you can do to increase the limit.
您可以进行注册表调整来增加限制。
For more information see http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx
有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx
回答by Leonardo Maxson
You should use one of the System.Diagnostics.Process.Start()
overloads. It's quite simple!
您应该使用其中一种System.Diagnostics.Process.Start()
重载。这很简单!
If you don't place the filename of the process you want to run (explorer.exe
), the system will recognize it as a valid folder path and try to attach it to the already running Explorer process. In this case, if the folder is already open, Explorer will do nothing.
如果您没有放置要运行的进程的文件名 ( explorer.exe
),系统会将其识别为有效的文件夹路径并尝试将其附加到已运行的 Explorer 进程。在这种情况下,如果文件夹已经打开,资源管理器将不执行任何操作。
If you place the filename of the process (as you did), the system will try to run a new instance of the process, passing the second string as a parameter. If the string is a valid folder, it is opened on the newly created process, if not, the new process will do nothing.
如果您放置进程的文件名(如您所做的那样),系统将尝试运行该进程的新实例,将第二个字符串作为参数传递。如果该字符串是有效文件夹,则在新创建的进程上打开它,如果不是,则新进程将不执行任何操作。
I don't know how invalid folder paths are treated by the process in any case. Using System.IO.Directory.Exists()
should be enough to ensure that.
在任何情况下,我都不知道进程如何处理无效的文件夹路径。使用System.IO.Directory.Exists()
应该足以确保。