如何可靠地启动应用程序? [英] How to reliably launch an application?

查看:52
本文介绍了如何可靠地启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试启动pdf文件(在本例中)。当用户单击按钮时,测试应用程序从TextBox获取pdf的路径

。我有一个可以在我的电脑上运行的
编写的代码,但我怀疑我的方法是

通用。


基本上,在HKEY_CLASSES_ROOT中,我查找.pdf扩展名并获取

人类可读类ID。然后我仔细查看,通过树递归

直到我得到命令然后返回该值。这似乎适用于

Adob​​e Acrobat Reader ...


必须有一些Windows用来查找应用程序的通用方法

当用户双击文件时。或者,.NET

框架中有什么东西可以实现这个目标吗?


这是我的代码:


private void OnViewFileClick(object sender,System.EventArgs e)

{

string fName = tbFile.Text.Trim();


if(!File.Exists(fName))

{

MessageBox.Show(this," File" + fName +"不存在)" ,文件IO

错误);

返回;

}


字符串ReaderPath = String.Empty;

尝试

{

RegistryKey key = Registry.ClassesRoot;

RegistryKey keyPDF = key .OpenSubKey(" .pdf",false);

string [] classNames = keyPDF.GetSubKeyNames();


if(classNames == null)

{

MessageBox.Show(此,没有与.pdf相关的应用程序

扩展名);

返回;

}


//这似乎不会得到所有情况......

RegistryKey keyPdfApp = key.OpenSubKey(classNames [0],false);

ReaderPath = GetCommand(keyPdfApp);

}

catch

{

MessageBox.Show(this,Error:找不到Adobe Acrobat Reader,

启动错误);

}


尝试

{

//突破命令字符串来自ReaderPath字符串

string [] path = ReaderPath.Split(new char [] {''\"'}};


/ /在命令行中使用.pdf文件启动阅读器

System.Diagnostics.Process.Start(path [1],fName);

}

catch(exception ex)

{

MessageBox.Show(ex.Message);

}

}

//返回命令字符串或String.Empty

私有字符串GetCommand(RegistryKey键)

{

string [] names = key.GetSubKeyNames();


if(names == null)

return String.Empty ;


foreach(名称中的字符串名称)

{

RegistryKey subkey = key.OpenSubKey(name,false);


string [] substrings =(subkey.Name).Split(new char [] {''\\''});


//看看这是否是以'command'结尾的关键字

if(substrings.Length-1] ==" command" )

{

//返回默认值

return((string)subkey.GetValue(""));

}


//找不到 - 递归子键

string command = GetCommand(subkey);

if(command!= String.Empty)

返回命令;

}


返回String.Empty;

}


感谢您的帮助。


Alfredo

解决方案

听起来很方便到达那里...... :)


如何使用:


System.Diagnostics.Process.Start(tbFile.Text.Trim());


这将打开应用程序或与文件关联的应用程序

并打开文件。


" David Rose" <沓******** @ rcn.com>在消息中写道

新闻:eS ************** @ tk2msftngp13.phx.gbl ...


我正在尝试启动pdf文件(在本例中)。当用户单击按钮时,测试应用程序
从TextBox获取pdf的路径。我已经编写了可以在我的计算机上运行的代码,但我怀疑我的方法
是通用的。

基本上,在HKEY_CLASSES_ROOT中,我查找.pdf扩展名并获取
人类可读的类ID。然后我仔细查看,通过树递归
直到我得到命令然后返回该值。对于Adobe Acrobat Reader来说,这似乎是
...

当用户双击文件时,Windows必须使用一些通用方法来查找
应用程序。或者,.NET
Framework中有什么东西可以实现这个目标吗?

这是我的代码:

private void OnViewFileClick(对象发送者,System.EventArgs e) )
{
字符串fName = tbFile.Text.Trim();
如果(!File.Exists(fName))
{MessageBox。显示(此,文件+fName +不存在,文件IO
错误);
返回;
}

string ReaderPath = String.Empty;
尝试
{/> RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(" .pdf",false);
string [] classNames = keyPDF.GetSubKeyNames();

if(classNames == null)
{MessageBox.Show(this," No application with a associated .pdf
扩展名);
返回;
}
//这似乎不会得到所有案例...
RegistryKey keyPdfApp = key.OpenSubKey(classNames [0],false);
ReaderPath = GetCommand(keyPdfApp);
}
{/> MessageBox.Show(这个,错误:找不到Adobe Acrobat Reader,
"启动错误);
}
尝试
//从ReaderPath字符串中删除命令字符串
string [] path = ReaderPath。 Split(new char [] {''\'"'}};

//在命令行中使用.pdf文件启动阅读器
System.Diagnostics.Process。开始(路径[1],fName);
}
catch(Exception ex)
{MessageBox.Show(ex.Message);
}
}

//返回命令字符串或String.Empty
私有字符串GetCommand(RegistryKey键)
{
string [] names = key.GetSubKeyNames() ;

if(names == null)
返回String.Empty;

foreach(名称中的字符串名称)
{
RegistryKey subkey = key.OpenSubKey(name,false);

string [] subs trings =(subkey.Name).Split(new char [] {''\\''}};

//看看这是否是以'command'结尾的关键字
if(substrings.Length-1] ==" command" )
//返回默认值
return((string)subkey.GetValue(""));
}

/ / not found - 递归子键
string command = GetCommand(subkey);
if(command!= String.Empty)
return命令;
}

返回String.Empty;
}
感谢您的帮助。

Alfredo



嗯...


我,嗯,以为你必须......嗯,你知道......我的意思是...像

这样做了吗?


男孩,我觉得很傻。


谢谢。


" Jim Hughes" < NO ********* @ Hotmail.com>在消息中写道

新闻:#W ************** @ tk2msftngp13.phx.gbl ...

听起来很像长到达那里的方法...... :)

如何使用:

System.Diagnostics.Process.Start(tbFile.Text.Trim());

这将打开应用程序或与文件相关的应用程序
并打开文件。

David Rose <沓******** @ rcn.com>在消息中写道
新闻:eS ************** @ tk2msftngp13.phx.gbl ...


我正在尝试启动pdf文件(在本例中)。测试应用程序


当用户单击按钮时,

从TextBox获取pdf的路径。我b $ b已经编写了可以在我的计算机上运行的代码,但我怀疑我的方法


通用。

基本上,在HKEY_CLASSES_ROOT中,我查找.pdf扩展名并获得
人类可读的类ID。然后我查看它,通过
树递归,直到我得到命令。然后返回该值。对于

Adob​​e Acrobat Reader,这似乎有效




必须有一些Windows使用的常规方法来查找
application

。或者,
..NET Framework中有什么东西可以实现这个目标吗?

这是我的代码:

private void OnViewFileClick(对象发送者,System.EventArgs) e)
{> string fName = tbFile.Text.Trim();

if(!File.Exists(fName))
{MessageBox} .Show(this,File+ fName +"不存在,文件IO
错误);
返回;
}
string ReaderPath = String.Empty;
尝试
{/> RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(" .pdf",false);
string [] classNames = keyPDF.GetSubKeyNames();

if(classNames == null)
{MessageBox.Show(this," No application associated with associated .pdf
扩展名);
返回;
}
//这似乎不会得到所有案例...
RegistryKey keyPdfApp = key.OpenSubKey(classNames [0],false);
ReaderPath = GetCommand(keyPdfApp);
}
{/> MessageBox.Show(这个,错误:找不到Adobe Acrobat Reader,
"启动错误);
}
尝试
//从ReaderPath字符串中删除命令字符串
string [] path = ReaderPath。 Split(new char [] {''\'"'}};

//在命令行中使用.pdf文件启动阅读器
System.Diagnostics.Process。开始(路径[1],fName);
}
catch(Exception ex)
{MessageBox.Show(ex.Message);
}
}

//返回命令字符串或String.Empty
私有字符串GetCommand(RegistryKey键)
{
string [] names = key.GetSubKeyNames() ;

if(names == null)
返回String.Empty;

foreach(名称中的字符串名称)
{
RegistryKey subkey = key.OpenSubKey(name,false);

string [] subs trings =(subkey.Name).Split(new char [] {''\\''}};

//看看这是否是以'command'结尾的关键字
if(substrings.Length-1] ==" command" )
//返回默认值
return((string)subkey.GetValue(""));
}

/ / not found - 递归子键
string command = GetCommand(subkey);
if(command!= String.Empty)
return命令;
}

返回String.Empty;
}
感谢您的帮助。

Alfredo




也许是这样......但我不会告诉任何人! :)


但看看你学到了多少!


David Rose <沓******** @ rcn.com>在消息中写道

新闻:OC ************** @ tk2msftngp13.phx.gbl ...

嗯...

我,嗯,以为你必须......嗯,你知道......我的意思是...工作
就像那样吗?

男孩我觉得自己很傻。

谢谢。

Jim Hughes < NO ********* @ Hotmail.com>在消息中写道
新闻:#W ************** @ tk2msftngp13.phx.gbl ...

听起来很长的路要走... :)

如何使用:

System.Diagnostics.Process.Start(tbFile.Text.Trim());

这将打开与
文件关联的应用程序或应用程序并打开文件。




< snip>


Hi,

I am trying to launch pdf files (in this case). The test application takes
the path to the pdf from a TextBox when the user clicks a button. I have
written code that works on my computer, but I have doubts that my method is
universal.

Basically, in HKEY_CLASSES_ROOT, I look up the .pdf extension and get the
human readable class id. Then I look that up, recursing through the tree
until I get to "command" and then return that value. This seems to work for
the Adobe Acrobat Reader...

There must be some general method that Windows uses to find the application
when the user double clicks on a file. Or, is there something in the .NET
Framework to accomplish this?

Here is my code:

private void OnViewFileClick(object sender, System.EventArgs e)
{
string fName = tbFile.Text.Trim();

if ( !File.Exists(fName) )
{
MessageBox.Show(this, "File " + fName + " does not exist", "File IO
Error");
return;
}

string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(".pdf", false);
string [] classNames = keyPDF.GetSubKeyNames();

if ( classNames == null )
{
MessageBox.Show(this, "No application associated with the .pdf
extension");
return;
}

// this seems like it would not get all cases...
RegistryKey keyPdfApp = key.OpenSubKey(classNames[0], false);
ReaderPath = GetCommand(keyPdfApp);
}
catch
{
MessageBox.Show(this, "Error: could not find Adobe Acrobat Reader",
"Launch Error");
}

try
{
// break out the command string from the ReaderPath string
string [] path = ReaderPath.Split(new char[] {''\"''});

// launch the reader with the .pdf file in the command line
System.Diagnostics.Process.Start(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(RegistryKey key)
{
string [] names = key.GetSubKeyNames();

if ( names == null )
return String.Empty;

foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey(name, false);

string [] substrings = (subkey.Name).Split(new char [] {''\\''});

// see if this is the key ending in "command"
if ( substrings[substrings.Length-1] == "command" )
{
// return the default value
return ((string)subkey.GetValue(""));
}

// not found - recurse the subkey
string command = GetCommand(subkey);
if ( command != String.Empty )
return command;
}

return String.Empty;
}

Thanks for any help.

Alfredo

解决方案

Sounds like the long way to get there ... :)

How about using:

System.Diagnostics.Process.Start(tbFile.Text.Trim( ));

This will open the application or the application associated with the file
and open the file.

"David Rose" <Da********@rcn.com> wrote in message
news:eS**************@tk2msftngp13.phx.gbl...

Hi,

I am trying to launch pdf files (in this case). The test application takes the path to the pdf from a TextBox when the user clicks a button. I have
written code that works on my computer, but I have doubts that my method is universal.

Basically, in HKEY_CLASSES_ROOT, I look up the .pdf extension and get the
human readable class id. Then I look that up, recursing through the tree
until I get to "command" and then return that value. This seems to work for the Adobe Acrobat Reader...

There must be some general method that Windows uses to find the application when the user double clicks on a file. Or, is there something in the .NET
Framework to accomplish this?

Here is my code:

private void OnViewFileClick(object sender, System.EventArgs e)
{
string fName = tbFile.Text.Trim();

if ( !File.Exists(fName) )
{
MessageBox.Show(this, "File " + fName + " does not exist", "File IO
Error");
return;
}

string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(".pdf", false);
string [] classNames = keyPDF.GetSubKeyNames();

if ( classNames == null )
{
MessageBox.Show(this, "No application associated with the .pdf
extension");
return;
}

// this seems like it would not get all cases...
RegistryKey keyPdfApp = key.OpenSubKey(classNames[0], false);
ReaderPath = GetCommand(keyPdfApp);
}
catch
{
MessageBox.Show(this, "Error: could not find Adobe Acrobat Reader",
"Launch Error");
}

try
{
// break out the command string from the ReaderPath string
string [] path = ReaderPath.Split(new char[] {''\"''});

// launch the reader with the .pdf file in the command line
System.Diagnostics.Process.Start(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(RegistryKey key)
{
string [] names = key.GetSubKeyNames();

if ( names == null )
return String.Empty;

foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey(name, false);

string [] substrings = (subkey.Name).Split(new char [] {''\\''});

// see if this is the key ending in "command"
if ( substrings[substrings.Length-1] == "command" )
{
// return the default value
return ((string)subkey.GetValue(""));
}

// not found - recurse the subkey
string command = GetCommand(subkey);
if ( command != String.Empty )
return command;
}

return String.Empty;
}

Thanks for any help.

Alfredo



Well...

I, um, thought that you had to... well, you know... I mean... Works like
that does it?

Boy, do I feel silly.

Thanks.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:#W**************@tk2msftngp13.phx.gbl...

Sounds like the long way to get there ... :)

How about using:

System.Diagnostics.Process.Start(tbFile.Text.Trim( ));

This will open the application or the application associated with the file
and open the file.

"David Rose" <Da********@rcn.com> wrote in message
news:eS**************@tk2msftngp13.phx.gbl...

Hi,

I am trying to launch pdf files (in this case). The test application


takes

the path to the pdf from a TextBox when the user clicks a button. I have written code that works on my computer, but I have doubts that my method


is

universal.

Basically, in HKEY_CLASSES_ROOT, I look up the .pdf extension and get the human readable class id. Then I look that up, recursing through the tree until I get to "command" and then return that value. This seems to work


for

the Adobe Acrobat Reader...

There must be some general method that Windows uses to find the


application

when the user double clicks on a file. Or, is there something in the ..NET Framework to accomplish this?

Here is my code:

private void OnViewFileClick(object sender, System.EventArgs e)
{
string fName = tbFile.Text.Trim();

if ( !File.Exists(fName) )
{
MessageBox.Show(this, "File " + fName + " does not exist", "File IO
Error");
return;
}

string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(".pdf", false);
string [] classNames = keyPDF.GetSubKeyNames();

if ( classNames == null )
{
MessageBox.Show(this, "No application associated with the .pdf
extension");
return;
}

// this seems like it would not get all cases...
RegistryKey keyPdfApp = key.OpenSubKey(classNames[0], false);
ReaderPath = GetCommand(keyPdfApp);
}
catch
{
MessageBox.Show(this, "Error: could not find Adobe Acrobat Reader",
"Launch Error");
}

try
{
// break out the command string from the ReaderPath string
string [] path = ReaderPath.Split(new char[] {''\"''});

// launch the reader with the .pdf file in the command line
System.Diagnostics.Process.Start(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(RegistryKey key)
{
string [] names = key.GetSubKeyNames();

if ( names == null )
return String.Empty;

foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey(name, false);

string [] substrings = (subkey.Name).Split(new char [] {''\\''});

// see if this is the key ending in "command"
if ( substrings[substrings.Length-1] == "command" )
{
// return the default value
return ((string)subkey.GetValue(""));
}

// not found - recurse the subkey
string command = GetCommand(subkey);
if ( command != String.Empty )
return command;
}

return String.Empty;
}

Thanks for any help.

Alfredo




Maybe so... But I won''t tell anyone! :)

But look at how much you learned!

"David Rose" <Da********@rcn.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...

Well...

I, um, thought that you had to... well, you know... I mean... Works like that does it?

Boy, do I feel silly.

Thanks.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:#W**************@tk2msftngp13.phx.gbl...

Sounds like the long way to get there ... :)

How about using:

System.Diagnostics.Process.Start(tbFile.Text.Trim( ));

This will open the application or the application associated with the file and open the file.



<snip>


这篇关于如何可靠地启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆