使用 Process.Start 打开文件夹
我看到了其他主题,并且遇到了另一个问题。 该过程正在启动(在任务管理器中看到),但该文件夹未在我的屏幕上打开。 怎么了?
System.Diagnostics.Process.Start("explorer.exe", @"c:\teste");
I saw the other topic and 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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您是否确定文件夹“
c:\teste
”存在? 如果没有,资源管理器将打开并显示一些默认文件夹(在我的例子中为“C:\Users\[用户名]\Documents
”)。更新
我尝试了以下变体:
如果这些(好吧,除了抛出异常的那个)在您的计算机上都不起作用,我认为问题不在于代码,而在于环境。 如果是这种情况,我将尝试以下一项(或两项):
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
").Update
I have tried the following variations:
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:
为了完整起见,如果您只想打开一个文件夹,请使用以下命令:
确保 FileName 以
Path.DirectorySeparatorChar
结尾,以使其明确指向文件夹。 (感谢@binki。)此解决方案不适用于打开文件夹和选择项目,因为似乎没有动词。
Just for completeness, if all you want to do is to open a folder, use this:
Ensure FileName ends with
Path.DirectorySeparatorChar
to make it unambiguously point to a folder. (Thanks to @binki.)This solution won't work for opening a folder and selecting an item, since there doesn't seem a verb for that.
如果您想选择文件或文件夹,可以使用以下命令:
If you want to select the file or folder you can use the following:
使用非转义字符串时不需要双反斜杠:
You don't need the double backslash when using unescaped strings:
您使用的是 @ 符号,这样就无需转义反斜杠。
删除 @ 或将 \\ 替换为 \
You're using the @ symbol, which removes the need for escaping your backslashes.
Remove the @ or replace \\ with \
您应该使用 System.Diagnostics.Process.Start() 重载之一。 这很简单!
如果您没有输入要运行的进程的文件名 (
explorer.exe
),系统会将其识别为有效的文件夹路径,并尝试将其附加到已运行的 Explorer 进程。 在这种情况下,如果文件夹已经打开,资源管理器将不执行任何操作。如果您放置进程的文件名(就像您所做的那样),系统将尝试运行该进程的新实例,并将第二个字符串作为参数传递。 如果该字符串是有效的文件夹,则在新创建的进程上打开它,如果不是,则新进程将不执行任何操作。
我不知道在任何情况下该进程如何处理无效的文件夹路径。 使用 System.IO.Directory.Exists() 应该足以确保这一点。
You should use one of the
System.Diagnostics.Process.Start()
overloads. It's quite simple!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.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.使用该方法的重载版本,该方法采用 ProcessStartInfo 实例并将 ProcessWindowStyle 属性设置为适合您的值。
Use an overloaded version of the method that takes a ProcessStartInfo instance and set the ProcessWindowStyle property to a value that works for you.
当 at 符号为您执行此操作时,您就可以转义反斜杠。
You're escaping the backslash when the at sign does that for you.
这段代码在 VS2010 环境中工作正常,并正确打开本地文件夹,但如果您在 IIS 中托管相同的应用程序并尝试打开,那么它肯定会失败。
This code works fine from the VS2010 environment and opens the local folder properly, but if you host the same application in IIS and try to open then it will fail for sure.
我刚刚遇到了这个问题,我找到了原因。 我的原因没有在这里列出,所以其他遇到此问题的人都没有解决它。
如果您以其他用户身份运行 Visual Studio 并尝试使用 Process.Start,它将在该用户上下文中运行,并且您不会在屏幕上看到它。
Ive just had this issue, and i found out why. my reason isnt listed here so anyone else who gets this issue and none of these fix it.
If you run Visual Studio as another user and attempt to use Process.Start it will run in that users context and you will not see it on your screen.
当您从开始菜单运行“explorer.exe c:\teste”时,它是否可以正确打开? 你尝试这个有多久了? 当我的机器有很多进程并且当我打开一个新进程(设置为 IE)时,我会看到类似的行为。它在任务管理器中启动,但不会显示在前端。 您尝试过重启吗?
以下代码应打开一个新的资源管理器实例
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?
The following code should open a new explorer instance
当您尝试此操作时,是否有很多应用程序正在运行?
有时我在工作中会遇到奇怪的行为,因为我的系统耗尽了 GDI 句柄,因为我打开了很多窗口(我们的应用程序使用了很多)。
发生这种情况时,窗口和上下文菜单将不再出现,直到我关闭某些内容以释放一些 GDI 句柄。
XP 和 Vista 中的默认限制为 10000。
我的 DevStudio 有 1500 个 GDI 句柄并不少见,因此如果您打开了几个 Dev studio 副本,它很快就会耗尽它们。 您可以在 TaskManager 中添加一列来查看每个进程正在使用多少个句柄。
您可以通过注册表调整来增加限制。
有关详细信息,请参阅 http://msdn.microsoft.com /en-us/library/ms724291(VS.85).aspx
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).
When this happens, windows and context menus no long appear until I close something to free up some GDI handles.
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.
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
System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
只需更改路径或在
字符串
中声明它System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
Just change the path or declare it in a
string