获取正在运行的应用程序 [英] Get location of running application
问题描述
我想通过使用C#语言来获取文件的位置(例如,Word,Powerpoint或Image ...)。我怎么做,我试图找到一个解决方案,但大多数回答项目的当前路径。
为了清楚地提出我的问题,我有一个例子如下。
我打开了三个powerpoint文件:包括p1.pptx,p2.pptx,p3.pptx。位置:D:/doc1/p1.pptx,D:/doc2/p2.pptx,D:/doc3/p3.pptx。
我想写一个函数(C#)来获取这些路径。
这意味着如果我定义:
I want to get the location of files are opening (For e.g: Word, Powerpoint, or Image ...) by using C# language. How I can do it, I have tried to find a solution but most of answer for project's current path.
To make my question clearly, I have a example as following.
I have opened three powerpoint files: include p1.pptx, p2.pptx, p3.pptx. with location : D:/doc1/p1.pptx, D:/doc2/p2.pptx, D:/doc3/p3.pptx.
I want to write a function ( C#) to get these path.
It mean if I define:
string path1,path2,path3;
然后我的函数的结果可以得到:
then result of my function can get:
path1="D:/doc1/p1.pptx";
path2="D:/doc2/p2.pptx";
path3="D:/doc3/p3.pptx";
先谢谢!
Thanks in advanced!
推荐答案
Determine the Executing Application’s Path (C#)
Start Microsoft Visual Studio .NET.
Create a new Visual C# Smart Device Application project named AppPath.
In the Smart Application Wizard, select the Pocket PC platform and Windows Application project type.
Add a Button control to the form. The button is named button1 by default.
Double click button1 to create a Click event handler
Add the following code to the event handler:
private void button1_Click(object sender, System.EventArgs e)
{
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
MessageBox.Show( path );
}
Refer to the following
http://msdn.microsoft.com/en-us/library/aa457089.aspx [ ^ ]
这对我来说没有意义:你不能打开一个文件,直到你有一个有效路径:在设计时你知道;或者,在运行时,使用OpenFile对话框选择。当然,您可以使用带有文件路径的ProcessStart打开注册为文件类型的默认执行上下文的应用程序(如果有)。
我认为你可能会发现当前默认应用程序的位置,如果在桌面或Windows资源管理器窗口中双击它将打开某个类型的文件。当然,文件类型的默认应用程序是用户可配置的。
我的机器上有多个视频播放器应用程序,我将VLC设置为默认应用程序打开.mkv文件,而不是Windows Media Player。
那么,如何找到给定文件类型的默认当前应用程序?
关联肯定保存在注册表中,但我强烈建议您避免直接操纵注册表,并使用以下技术之一:
1.使用Win API:[ ^ ]。
This doesn't quite make sense to me: you can't open a file until you have a valid path to the file: either known to you at design-time; or, at run-time, selected using an OpenFile dialog. Of course, you can use ProcessStart with a file-path to open the Application (if any) registered as the default execution context for the file-type.
I think what you may be after is finding out the location of the current default Application that will will open a file of a certain Type if it is double-clicked on the Desktop, or in an Windows Explorer window. Default applications for file types are, of course, user-configurable.
I have more than one video-player application on my machine, and I set VLC to be the default Application that opens .mkv files, rather than Windows Media Player.
So, how do you find the default current application for a given file-type ?
The associations are definitely kept in the Registry, but I strongly suggest you avoid any direct manipulation of the Registry, and use one of these techniques:
1. use the Win API: [^].
// required in addition to the usual WinForm 'usings
using System.Runtime.InteropServices;
private static bool HasExecutable(string path)
{
var executable = FindExecutable(path);
return !string.IsNullOrEmpty(executable);
}
private static string FindExecutable(string path)
{
var executable = new StringBuilder(1024);
var result = FindExecutable(path, string.Empty, executable);
return result >= 32 ? executable.ToString() : string.Empty;
}
[DllImport("shell32.dll", EntryPoint = "FindExecutable")]
private static extern long FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult);
private void btnTest_Click(object sender, EventArgs e)
{
string path = @"C:\Users\Uruk\Desktop\ckeditor\ckeditor_4.4.6_full.zip";
string openingApp = FindExecutable(path);
}
2。现在,如果你想枚举所有可以打开给定文件类型的应用程序(那些已经在Windows注册的应用程序):据我所知(我多年没有访问过这个区域) ,您必须阅读注册表,或使用AssocCreate(shlwapi)API:[ ^ ]。我没有尝试过这个API调用。
2. Now if you want to enumerate all the Applications that can open a given file type (those that have registered with Windows): to the best of my knowledge (and I haven't visited this area in years), you will have to read the Registry, or use the AssocCreate (shlwapi) API: [^]. I have not tried this API call.
这篇关于获取正在运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!