使用Process.Start打开文件夹

时间:2023-01-17 08:26:04

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");

14 个解决方案

#1


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 \ [用户名] \ 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
  • 打开“运行”对话框,输入“explorer.exe”并按Enter键

  • Open a command prompt, type "explorer.exe" and hit enter
  • 打开命令提示符,键入“explorer.exe”并按Enter键

#2


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"
});

This solution won't work for opening a folder and selecting an item, since there doesn't seem a verb for that.

此解决方案不适用于打开文件夹和选择项目,因为似乎没有动词。

#3


If you want to select the file or folder you can use the following:

如果要选择文件或文件夹,可以使用以下命令:

Process.Start("explorer.exe", "/select, c:\\teste");

#4


You're using the @ symbol, which removes the need for escaping your backslashes.

您正在使用@符号,这样就无需转义反斜杠。

Remove the @ or replace \\ with \

删除@或用\替换\\

#5


You don't need the double backslash when using unescaped strings:

使用非转义字符串时,不需要双反斜杠:

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

#6


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),系统会将其识别为有效的文件夹路径,并尝试将其附加到已运行的资源管理器进程。在这种情况下,如果文件夹已经打开,资源管理器将不执行任何操作。

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()应该足以确保。

#7


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属性设置为适合您的值。

#8


System.Diagnostics.Process.Start("explorer.exe",@"c:\teste"); 

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.

此代码可以在VS2010环境中正常工作并正确打开本地文件夹,但如果您在IIS中托管相同的应用程序并尝试打开,那么它肯定会失败。

#9


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.

如果您以另一个用户身份运行Visual Studio并尝试使用Process.Start,它将在该用户上下文中运行,您将无法在屏幕上看到它。

#10


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.

您说任务管理器中会出现另一个资源管理器副本,但您无法看到它。

Is it possible that it is opening offscreen (ie another monitor)?

是否有可能在屏幕外打开(即另一台显示器)?

Or are you by any chance doing this in a non-interactive service?

或者您是否有机会在非交互式服务中这样做?

#11


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");
}
}

#12


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 Handles,因为我有很多窗口打开(我们的应用程序使用很多)。

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

#13


You're escaping the backslash when the at sign does that for you.

当at符号为你做的时候,你正在逃避反斜杠。

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

#14


System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

Just change the path or declare it in a string

只需更改路径或在字符串中声明它

#1


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 \ [用户名] \ 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
  • 打开“运行”对话框,输入“explorer.exe”并按Enter键

  • Open a command prompt, type "explorer.exe" and hit enter
  • 打开命令提示符,键入“explorer.exe”并按Enter键

#2


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"
});

This solution won't work for opening a folder and selecting an item, since there doesn't seem a verb for that.

此解决方案不适用于打开文件夹和选择项目,因为似乎没有动词。

#3


If you want to select the file or folder you can use the following:

如果要选择文件或文件夹,可以使用以下命令:

Process.Start("explorer.exe", "/select, c:\\teste");

#4


You're using the @ symbol, which removes the need for escaping your backslashes.

您正在使用@符号,这样就无需转义反斜杠。

Remove the @ or replace \\ with \

删除@或用\替换\\

#5


You don't need the double backslash when using unescaped strings:

使用非转义字符串时,不需要双反斜杠:

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

#6


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),系统会将其识别为有效的文件夹路径,并尝试将其附加到已运行的资源管理器进程。在这种情况下,如果文件夹已经打开,资源管理器将不执行任何操作。

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()应该足以确保。

#7


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属性设置为适合您的值。

#8


System.Diagnostics.Process.Start("explorer.exe",@"c:\teste"); 

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.

此代码可以在VS2010环境中正常工作并正确打开本地文件夹,但如果您在IIS中托管相同的应用程序并尝试打开,那么它肯定会失败。

#9


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.

如果您以另一个用户身份运行Visual Studio并尝试使用Process.Start,它将在该用户上下文中运行,您将无法在屏幕上看到它。

#10


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.

您说任务管理器中会出现另一个资源管理器副本,但您无法看到它。

Is it possible that it is opening offscreen (ie another monitor)?

是否有可能在屏幕外打开(即另一台显示器)?

Or are you by any chance doing this in a non-interactive service?

或者您是否有机会在非交互式服务中这样做?

#11


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");
}
}

#12


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 Handles,因为我有很多窗口打开(我们的应用程序使用很多)。

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

#13


You're escaping the backslash when the at sign does that for you.

当at符号为你做的时候,你正在逃避反斜杠。

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

#14


System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");

Just change the path or declare it in a string

只需更改路径或在字符串中声明它