在资源管理器中打开文件夹并选择文件
我正在尝试在资源管理器中打开一个文件夹并选择一个文件。
以下代码会产生文件未找到异常:
System.Diagnostics.Process.Start(
"explorer.exe /select,"
+ listView1.SelectedItems[0].SubItems[1].Text + "\\"
+ listView1.SelectedItems[0].Text);
How can I get this command to run in C#?
I'm trying to open a folder in explorer with a file selected.
The following code produces a file not found exception:
System.Diagnostics.Process.Start(
"explorer.exe /select,"
+ listView1.SelectedItems[0].SubItems[1].Text + "\\"
+ listView1.SelectedItems[0].Text);
How can I get this command to execute in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用此方法:
第一个参数是一个应用程序(explorer.exe) ,第二个方法参数是您运行的应用程序的参数。
例如:
在 CMD 中:
在 C# 中:
Use this method:
First argument is an application (explorer.exe), second method argument are arguments of the application you run.
For example:
in CMD:
in C#:
只是我的 2 美分价值,如果您的文件名包含空格,即“c:\My File Contains Spaces.txt”,您需要用引号将文件名引起来,否则资源管理器将假定其他单词是不同的参数...
Just my 2 cents worth, if your filename contains spaces, ie "c:\My File Contains Spaces.txt", you'll need to surround the filename with quotes otherwise explorer will assume that the othe words are different arguments...
奇怪的是,在
explorer.exe
上使用Process.Start
以及/select
参数仅适用于长度小于 120 个字符的路径。我必须使用本机 Windows 方法才能使其在所有情况下都能工作:
Using
Process.Start
onexplorer.exe
with the/select
argument oddly only works for paths less than 120 characters long.I had to use a native windows method to get it to work in all cases:
Samuel Yang 的回答让我绊倒了,这是我的 3 美分价值。
Adrian Hum 是对的,请确保在文件名两边加上引号。 并不是因为它不能像 zourtney 指出的那样处理空格,而是因为它会将文件名中的逗号(可能还有其他字符)识别为单独的参数。
所以它应该看起来像 Adrian Hum 建议的那样。
Samuel Yang answer tripped me up, here is my 3 cents worth.
Adrian Hum is right, make sure you put quotes around your filename. Not because it can't handle spaces as zourtney pointed out, but because it will recognize the commas (and possibly other characters) in filenames as separate arguments.
So it should look as Adrian Hum suggested.
使用“/select,c:\file.txt”
注意/select后面应该有一个逗号而不是空格。
Use "/select,c:\file.txt"
Notice there should be a comma after /select instead of space..
找不到文件的最可能原因是路径中有空格。例如,它找不到“explorer /select,c:\space space\space.txt”。
只需在路径前后添加双引号即可,例如;
或者在 C# 中执行相同的操作
The most possible reason for it not to find the file is the path having spaces in. For example, it won't find "explorer /select,c:\space space\space.txt".
Just add double quotes before and after the path, like;
or do the same in C# with
您需要将要传递的参数(“/select 等”)放在 Start 方法的第二个参数中。
You need to put the arguments to pass ("/select etc") in the second parameter of the Start method.
这可能有点过分了,但我喜欢方便的函数,所以采用这个:
这是我用作.Quote() 的扩展函数:
It might be a bit of a overkill but I like convinience functions so take this one:
This is the extension function I use as <string>.Quote():
基于 Jan Croonen 的简单 C# 9.0 方法答案:
Simple C# 9.0 method based on Jan Croonen's answer:
来晚会了。
我发现 RandomEngy 的解决方案很有用,但对其进行了轻微修改以允许一次选择多个文件。 希望有人觉得它有用。
Coming to the party late.
I found RandomEngy's solution useful, but modified it slightly to allow the selection of many files at once. Hope someone finds it useful.
如果您的路径包含逗号,则在使用 Process.Start(ProcessStartInfo) 时可以在路径周围加上引号。
但是,当使用 Process.Start(string, string) 时它将不起作用。 看起来 Process.Start(string, string) 实际上删除了参数内的引号。
这是一个对我有用的简单示例。
If your path contains comma's, putting quotes around the path will work when using Process.Start(ProcessStartInfo).
It will NOT work when using Process.Start(string, string) however. It seems like Process.Start(string, string) actually removes the quotes inside of your args.
Here is a simple example that works for me.