我正在用 C# 和 MATLAB 制作一个软件,它调用另一个软件 (CMG) 来进行一些处理。我的问题是我放入程序中的软件地址只在我的个人电脑上正确,在客户的电脑上不正确(我不知道 CMG 软件在他们的电脑上的路径是什么)。
我怎样才能提供地址的一般形式以使其适用于每台计算机?
以下是我从我的MATLAB软件中调用的路径:
C:\Program Files (x86)\CMG\STARS\2011.10\Win_x64\EXE\st201110.exe
如你所见,它在C盘,版本是2011.10。因此,如果客户的版本是其他版本并且安装在其他驱动器上,则此路径没有意义。
最佳答案
注册表项 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 提供了大多数应用程序安装位置的列表:
注意:它没有列出 PC 上的所有 EXE 应用程序,因为有些应用程序不需要安装。
在你的情况下,我很确定 CMG STARS 将被列出,你将能够通过遍历所有查看 DisplayName 值并获取 InstallLocation< 的子项来搜索它/strong="">.
另请注意,此卸载注册表项存在于注册表中的 3 个位置: 这是一个返回应用程序安装位置的类: 这是你如何调用它: 要获取 nameOfAppToFind,您需要在注册表中查看 DisplayName: REF: 我从 here 修改了上面的代码返回安装路径。 您还可以使用系统管理 .Net DLL 来获取 InstallLocation,尽管它的速度非常慢,并且会为您系统上安装的每个产品创建“Windows Installer 重新配置产品”事件日志消息。 上述方法都不会告诉您可执行文件的名称,但是通过遍历安装路径中的所有文件并使用我讨论的技术 here to look at file properties 很容易计算出来。 以检测具有正确文件描述 的 EXE,例如: 无论您使用哪种方法(1 或 2),我都建议您保存 exe 名称的位置,这样您只需执行一次此操作。在我看来,最好使用方法 1,因为它更快并且不会创建所有“Windows Installer 重新配置产品”。事件日志。 如果正在安装您的应用程序,您可以在安装过程中找到 CMG STARS 所在的位置 Using Windows Installer to Inventory Products and Patches : Enumerating Products 在安装过程中,您会找到 CMG STARS 的 exe 路径,并使用该值保存一个注册表项。 我讨论使用 saving an EXE's install path in the registry for updating applications here 的这种方法. 如评论中所述,值得在注册表中搜索 EXE 的名称 st201110.exe 并查看 CMG STAR 应用程序的作者是否已在注册表中提供此信息您可以直接访问的 key 。 如果所有其他方法均失败,则向用户显示 FileOpenDialog 并让他们手动指定 exe 的路径。 我提到将安装路径和 exe 名称存储在注册表(或数据库、配置文件等)中,并且在对它进行任何外部调用之前,您应该始终检查 exe 文件是否存在,例如:
1. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall in CurrentUser
2. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall inside LocalMachine
3.SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\LocalMachine中的Uninstallusing Microsoft.Win32;
public static class InstalledApplications
{
public static string GetApplictionInstallPath(string nameOfAppToFind)
{
string installedPath;
string keyName;
// search in: CurrentUser
keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
installedPath = ExistsInSubKey(Registry.CurrentUser, keyName, "DisplayName", nameOfAppToFind);
if (!string.IsNullOrEmpty(installedPath))
{
return installedPath;
}
// search in: LocalMachine_32
keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
installedPath = ExistsInSubKey(Registry.LocalMachine, keyName, "DisplayName", nameOfAppToFind);
if (!string.IsNullOrEmpty(installedPath))
{
return installedPath;
}
// search in: LocalMachine_64
keyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
installedPath = ExistsInSubKey(Registry.LocalMachine, keyName, "DisplayName", nameOfAppToFind);
if (!string.IsNullOrEmpty(installedPath))
{
return installedPath;
}
return string.Empty;
}
private static string ExistsInSubKey(RegistryKey root, string subKeyName, string attributeName, string nameOfAppToFind)
{
RegistryKey subkey;
string displayName;
using (RegistryKey key = root.OpenSubKey(subKeyName))
{
if (key != null)
{
foreach (string kn in key.GetSubKeyNames())
{
using (subkey = key.OpenSubKey(kn))
{
displayName = subkey.GetValue(attributeName) as string;
if (nameOfAppToFind.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
return subkey.GetValue("InstallLocation") as string;
}
}
}
}
}
return string.Empty;
}
}
string installPath = InstalledApplications.GetApplictionInstallPath(nameOfAppToFind);
方法二
using System.Management;
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject mo in mos.Get())
{
Debug.Print(mo["Name"].ToString() + "," + mo["InstallLocation"].ToString() + Environment.NewLine);
}
获取EXE的名称
private string GetFileExeNameByFileDescription(string fileDescriptionToFind, string installPath)
{
string exeName = string.Empty;
foreach (string filePath in Directory.GetFiles(installPath, "*.exe"))
{
string fileDescription = GetSpecificFileProperties(filePath, 34).Replace(Environment.NewLine, string.Empty);
if (fileDescription == fileDescriptionToFind)
{
exeName = GetSpecificFileProperties(filePath, 0).Replace(Environment.NewLine, string.Empty);
break;
}
}
return exeName;
}
使用安装程序的替代方法
Use the MsiEnumProductsEx function to enumerate Windows Installer applications that are installed in the
system. This function can find all the per-machine installations and
per-user installations of applications (managed and unmanaged) for the
current user and other users in the system. Use the dwContext
parameter to specify the installation context to be found. You can
specify any one or any combination of the possible installation
contexts. Use the szUserSid parameter to specify the user context of
applications to be found.
提示
B计划
第三方应用卸载或升级怎么办?
if (!File.Exists(installPath + exeName))
{
//Run through the process to establish where the 3rd party application is installed
}
关于c# - 如何找到 EXE 的安装位置 - 正确的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26613336/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco