예제 #1
0
 /// <summary>
 /// 保存能力设置
 /// </summary>
 /// <param name="capId">能力标识</param>
 /// <param name="oneValue">能力值</param>
 public void SetSettings(TwCap capId, TwOneValue oneValue)
 {
     if (userSettings == null)
     {
         return;
     }
     userSettings[capId] = oneValue;
 }
      public TwCapability(TwCap cap, TwType valueType, object value)
      {
        CapabilityType = cap;
        ContainerType = TwOn.One;

        value = CastFromCapabilityType(value);

        TwOneValue container = new TwOneValue(valueType, value);

        Handle = LibKernel32.GlobalAlloc(0x42, Marshal.SizeOf(container));

        try
        {
          IntPtr pv = LibKernel32.GlobalLock(Handle);
          Marshal.StructureToPtr(container, pv, true);
        }
        finally
        {
          LibKernel32.GlobalUnlock(Handle);
        }
      }
예제 #3
0
        /// <summary>
        /// 保存设置到配置文件
        /// </summary>
        /// <returns>保存是否成功</returns>
        public bool SaveSettings()
        {
            string configPath = Config.GetUFileClientConfigFile();  //用户自定义能力配置文件路径

            //1:如果配置文件不存在返回
            if (!File.Exists(configPath))
            {
                logger.Error("配置文件不存在");
                return(false);
            }

            //2:如果扫描仪节点未设置则返回
            if (defaultScannerName == null)
            {
                logger.Error("默认扫描仪没有设置");
                return(false);
            }
            //二:保存配置函数
            try
            {
                //1 创建扫描能力记录节点
                XmlNode scannerEle = doc.CreateElement("scanner");

                //1.1创建扫描仪名称节点
                XmlElement scannerNameEle = doc.CreateElement("scannerName");
                scannerNameEle.InnerText = defaultScannerName;
                scannerEle.AppendChild(scannerNameEle);

                //1.2创建能力列表
                XmlElement  scannerCapListEle = doc.CreateElement("scannerCap");
                ICollection caps = userSettings.Keys;
                foreach (TwCap capID in caps)
                {
                    //1.2.1获取能力
                    string     capName  = capID.ToString();
                    TwOneValue oneValue = userSettings[capID] as TwOneValue;
                    if (oneValue == null || oneValue.ItemStr == null)
                    {
                        continue;
                    }
                    //1.2.2生成节点
                    XmlElement capEle = doc.CreateElement(capName);
                    capEle.SetAttribute("Itemtype", oneValue.ItemType.ToString());
                    capEle.SetAttribute("Item", oneValue.ItemStr);

                    //1.2.3添加到尾部
                    scannerCapListEle.AppendChild(capEle);
                }
                scannerEle.AppendChild(scannerCapListEle);


                //2.1读取配置文件中的不同扫描仪的配置节点列表
                XmlNode scanners = doc.SelectSingleNode("//ufileclient_config/scannerList");

                if (scanners == null)
                {
                    XmlNode    root    = doc.SelectSingleNode("//ufileclient_config");
                    XmlElement tmpList = doc.CreateElement("scannerList");
                    root.AppendChild(tmpList);
                    scanners = doc.SelectSingleNode("//ufileclient_config/scannerList");
                }

                //2.2查找配置文件中的默认扫描仪节点列表
                XmlNodeList scannerList = doc.SelectNodes("//ufileclient_config/scannerList/scanner");
                XmlNode     scannerNode = GetScannerSettins(defaultScannerName, scannerList);

                //2.3 如果节点存在则替换之
                if (scannerNode != null)
                {
                    scanners.ReplaceChild(scannerEle, scannerNode);
                }
                //2.3 节点不存在则创建新节点
                else
                {
                    scanners.AppendChild(scannerEle);
                }
            }
            #region handle the exception
            catch (Exception e)
            {
                logger.Error("配置文件操作失败:" + e.Message);
                return(false);
            }
            finally
            {
                if (doc != null)
                {
                    try
                    {
                        doc.Save(configPath);
                    }
                    catch (Exception e)
                    {
                        logger.Error("配置文件保存失败:" + e.Message);
                    }
                }
                ;
            }
            #endregion
            return(true);
        }
예제 #4
0
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <returns></returns>
        private bool LoadSettings()
        {
            try
            {
                //一:读取默认扫描仪名称
                string scannerName = defaultScannerName;
                //检查扫描仪名称是否存在
                if (scannerName == null)
                {
                    logger.Debug("没有选择扫描仪");
                    return(false);
                }

                //二:根据扫描仪名称读取节点配置
                XmlNodeList scannerNodeList = doc.SelectNodes(ConstCommon.scannerConfigXpath);

                //1:获得具有指定名称的配置节点
                XmlNode scannerNode = GetScannerSettins(scannerName, scannerNodeList);
                //1.1:如果不存在则返回
                if (scannerNode == null)
                {
                    logger.Debug("指定的扫描仪没有配置");
                    return(true);
                }
                //2:读取节点能力配置列表
                else
                {
                    XmlNodeList scannerChildList = scannerNode.ChildNodes;
                    if (scannerChildList[1] != null)
                    {
                        XmlNodeList capNodeList = scannerChildList[1].ChildNodes;

                        for (int j = 0; j < capNodeList.Count; j++)
                        {
                            XmlElement xmlElement = capNodeList[j] as XmlElement;

                            //2.1 获取节点数值
                            string capName  = xmlElement.Name;
                            string capType  = xmlElement.GetAttribute("Itemtype");
                            string capValue = xmlElement.GetAttribute("Item");

                            //2.2 生成oneValueItem对象
                            TwCap      capID    = ((TwCap)Enum.Parse(typeof(TwCap), capName));
                            TwType     dataType = (TwType)Enum.Parse(typeof(TwType), capType);
                            TwOneValue oneValue = new TwOneValue(dataType, capValue);

                            //2.4 记录扫描能力
                            userSettings[capID] = oneValue;
                        }
                    }
                }
            }
            #region  异常处理
            catch (System.Xml.XPath.XPathException e)
            {
                logger.Error("配置文件读取失败:" + e.Message);
                return(false);
            }
            catch (ArgumentException e)
            {
                logger.Error("配置文件读取失败:" + e.Message);
                return(false);
            }
            #endregion
            return(true);
        }