在可移动介质中安装项目取决于可移动介质名称 [英] Install project in removable media depend upon removable media name

查看:73
本文介绍了在可移动介质中安装项目取决于可移动介质名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我需要将项目目录安装到可移动光盘上。我不想在c:\programfiles \ ...........安装我的项目。我想将它安装在我的闪存或可移动媒体上。我的设置shuld检测我的可移动媒体的名称。并且当用户安装它时,将shold安装在那个特定名称的可移动媒体上。



我将采用桌面上的快捷方式。在安装用户尝试打开该应用程序之后,当存在特定名称的可移动介质时,它会被分解。如果该名称的可移动磁盘存在该应用程序不应该打开

解决方案

安装的整个概念不是为可拆卸的设计而设计的媒体。尽管如此,许多优质产品在可移动介质上运行良好,或者有两种不同的包装:安装和一种不安装,通常称为便携式,可以很好地用于可移动介质。



此外,许多优质或更好的产品原则上不需要安装。最后,我看到微软建议更喜欢这种自安装或无安装的产品。许多安装都是多余的,只会污染系统注册表。你什么时候需要安装?一个典型的情况是:您需要在系统中注册一些数据文件类型,就像Shell启动文件一样,通过其名称识别文件,例如* .myFileType。你真的需要它吗?



即使这些文件确实存在,你也不应该尝试做你想要达到的目标。首先,系统中可以有多个可移动驱动器。找到您的应用程序很简单,但通过名称查找它并不是一个可靠的选择;用户始终有机会在同一名称下拥有另一个可执行文件。我认为你应该检查你的应用程序和应用程序用户的概念。你真的应该假设你的用户不是白痴。如果某个用户将您的应用程序放在可移动媒体上,则该人员应该理解该媒体是可移动的。这样的用户将使用一些文件管理器(甚至命令行)打开可移动驱动器并单击应用程序,而不是某些数据文件。



更重要的是,标准某些软件产品的注册机制没有规定将注册申请放在任何地方,如果没有固定名称。要解决此限制,您必须安装一些其他软件,例如您的自定义应用程序启动器。我可以详细解释如何做到这一点,但它会破坏在可移动媒体上使用您的应用程序的目的以不需要安装的方式开发应用程序;这是你能做的最好的。







如果你需要找一些东西已知文件名,在可移动卷集中,出于某些合理目的,可能需要这样做。您可以执行以下操作:

  1. 使用WMI: http://msdn.microsoft.com/en-us/library/system.management.instrumentation%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.management.instrumentation%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。
  2. 使用Win32_DiskDrive类遍历所有驱动器。 System Capabilities属性将告诉您驱动器是否可移动;忽略不可移动的。记住Name属性的值。
  3. 使用Win32_DiskPartition类遍历所有分区;忽略没有文件系统的分区。
  4. 对于每个分区,通过属性DiskIndex查找其驱动器,忽略那些指向不可移动驱动器的驱动器。
  5. 使用以下方法遍历所有逻辑磁盘卷classWin32_LogicalDiskToPartition;它将为您提供与某个分区关联的每个卷的根名称。
  6. 创建分区集合,忽略那些不指向逻辑卷的分区;它将允许将某些分区子集与驱动器相关联(仅用于确定可移动驱动器上是否有分区)和逻辑卷的根路径,这是执行文件搜索所需的。
  7. 此类过滤后,您将获得可移动驱动器上分区的根目录名称的集合。在其中找到您的文件。





它可能是这样的:



  const   string  selectFormat =   SELECT * FROM {0}; 
const string wmiTypeDrive = Win32_DiskDrive;
const string wmiTypePartition = Win32_DiskPartition;
const string wmiTypeLogicalDriveToPartitionAssociation = Win32_LogicalDiskToPartition;

// ...

系统.Func< string,ManagementObjectCollection> getAllWmiObjects =(wmiType)= > {
ManagementObjectSearcher searcher =
new ManagementObjectSearcher ( string .Format(selectFormat,wmiType));
return searcher.Get();
}; // getAllWmiObjects
ManagementObjectCollection partitions = getAllWmiObjects(wmiTypePartition);
ManagementObjectCollection drives = getAllWmiObjects(wmiTypeDrive);
ManagementObjectCollection driveToPartitionAssociations =
getAllWmiObjects(wmiTypeLogicalDriveToPartitionAssociation);

// ...

< span class =code-keyword> foreach (ManagementObject drive in drives){
ushort [] capabilities =( ushort [])ManagementObject [ 能力];
// 这是你如何知道驱动器是否可拆卸
bool sRemovable = capabilities!= null && System.Array.FindIndex< ushort>(
功能,(元素)= > {返回 element == 7 ;})> = 0 ;
//
}
// 以后可以找到索引作为所有驱动器集合中驱动器的索引

// 依此类推......





这听起来很复杂,但整个代码只有几十行。



-SA

Hello,

I need to install the directory of my project to removable disc. i dont want to install my project in c:\programfiles\........... . i want to install it in on my flash memory or removable media. My setup shuld detect the name of my removable media. And when ever the user install it shold install in that perticular name removable media.

I will take the shortcut on desktop. after nstalling when ever user try to open that application it shold work when the removable media of perticular name is present. if the removable disk of that name is ot present that application should not open

解决方案

The whole concept of "installation" is not something which is designed for the use of removable media. Still, many good products work perfectly well on removable media or have two different kinds of packaging: installation and one without installation, which is usually called "portable" and can be well used on removable media.

Moreover, many good or even better products do not require installation in principle. Finally, I saw Microsoft recommendations to prefer such "self-installing" or "no installation" products. Many installations are redundant and only contaminate the system registry. When would you really need installation? One typical case is: you need to register some data file type(s) in the system the way your application would be automatically launched if the Shell starts the file, recognized by its name, such as "*.myFileType". Do you really need it?

Even if such files do exist, you should not try to do what you are trying to achieve. First of all, there can be more than one removable drive in the system. Finding your application is easy, but finding it by name is not a reliable options; the user always has a chance to have another executable file under the same name. I think what you should review your concept of the application and the application user. You really should assume that your user is not an idiot. If some user put your application on a removable media, such person is supposed to understand that the media is removable. Such user will use some file manager (or even command line) to open that removable drive and click on the application, not on some data file.

More importantly, standard mechanisms of registration of some software products do not make provision on putting a registered application anywhere where if won't have a fixed name. To work around this limitation, you would have to install some other software, something like your custom "application launcher". I could explain in detail how to do it, but it would defeat the purpose of using your application on the removable media. Develop your application the way not requiring installation; this is the best you can do.

[EDIT]

If you need to find something of known file name, in the set of removable volumes, this can be needed for some reasonable purposes. Here is what you can do:

  1. Use WMI: http://msdn.microsoft.com/en-us/library/system.management.instrumentation%28v=vs.110%29.aspx[^].
  2. Traverse all drives using the class "Win32_DiskDrive". System Capabilities property will tell you if the drive is removable or not; ignore non-removable ones. Remember values of the Name property.
  3. Traverse all partitions using the class "Win32_DiskPartition"; ignoring partitions without file systems.
  4. For each partition, find its drive by the property "DiskIndex", ignore those pointing to non-removable drive.
  5. Traverse all logical disk volumes using the class "Win32_LogicalDiskToPartition"; it will give you the root name of each volume associated with some partition.
  6. Create collection of partitions ignoring those not pointing to logical volume; it will allow to associate some subset of partitions with drives (just to determine if a partition on the removable drive or not) and root paths of logical volumes, which are needed to perform file search.
  7. After such filtering, you will obtain the collection of root directory names of the partitions on removable drives. Find your file(s) in it.



It could be something like:

const string selectFormat = "SELECT * FROM {0}";
const string wmiTypeDrive = "Win32_DiskDrive";
const string wmiTypePartition = "Win32_DiskPartition";
const string wmiTypeLogicalDriveToPartitionAssociation = "Win32_LogicalDiskToPartition";

//...

System.Func<string, ManagementObjectCollection> getAllWmiObjects = (wmiType) => {
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(string.Format(selectFormat, wmiType));
            return searcher.Get();
        }; //getAllWmiObjects
ManagementObjectCollection partitions = getAllWmiObjects(wmiTypePartition);
ManagementObjectCollection drives = getAllWmiObjects(wmiTypeDrive);
ManagementObjectCollection driveToPartitionAssociations =  
    getAllWmiObjects(wmiTypeLogicalDriveToPartitionAssociation);

//...

foreach (ManagementObject drive in drives) {
    ushort[] capabilities = (ushort[])ManagementObject["Capabilities"];
    // this is how you can find out if the drive is removable
    bool sRemovable = capabilities != null && System.Array.FindIndex<ushort>(
        capabilities, (element) => { return element == 7; }) >= 0;
    //
}
// index can later be found as index of the drive in the collection of all drives

// and so on...



It only sounds complicated, but the whole code will be few tens of lines.

—SA


这篇关于在可移动介质中安装项目取决于可移动介质名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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