Ejemplo n.º 1
0
        /// <summary>
        /// 将信息保存到文件中。
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="force">是否强制保存?(即使文件内容没有修改也更新文件)</param>
        /// <returns>成功/失败?</returns>
        public virtual bool Save(string fileName, bool force)
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                this.Name = Path.GetFileNameWithoutExtension(fileName);
            }

            string content = GetContent();

            if (content == null)
            {
                return(false);
            }

            if (!force && ViTextFile.CompareContent(content, fileName))
            {
                return(true);
            }

            try
            {
                using (StreamWriter writer = ViTextFile.CreateWriter(fileName))
                    return(this.Write(writer, content));
            }
            catch (Exception ee)
            {
                Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message);
                Trace.WriteLine("### " + ee.StackTrace);
            }
            return(false);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 得到信息的文本行描述。
 /// </summary>
 /// <returns>信息的文本行描述。</returns>
 public virtual string GetContent()
 {
     try
     {
         StringBuilder sbContent = new StringBuilder();
         using (StringWriter writer = ViTextFile.CreateWriter(sbContent))
         {
             if (this.Save(writer))
             {
                 return(sbContent.ToString());
             }
         }
     }
     catch (Exception ee)
     {
         Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message);
         Trace.WriteLine("### " + ee.StackTrace);
     }
     return(null);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 修改 POE 文件内部的类型名称。
        /// </summary>
        /// <param name="fileName">POE 文件名称</param>
        /// <param name="typeName">POE 类型名称</param>
        /// <returns>成功与否?</returns>
        public static bool ChangeName(string fileName, string typeName)
        {
            try
            {
                ArrayList alLines = new ArrayList();

                // 读取文件所有行
                using (StreamReader reader = new StreamReader(fileName, Encoding.Default))
                {
                    ViConnType connType = null;
                    string     oldName = null;
                    string     line, trim, type;
                    int        lineIndex = 0;
                    while (true)
                    {
                        line = reader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        ++lineIndex;

                        if (oldName == null)
                        {
                            trim = line.Trim();
                            if (trim.StartsWith("PROGRAM ", StringComparison.OrdinalIgnoreCase))
                            {
                                connType = ViConnType.Parse(trim.Substring(8), ViTypeCreation.None);
                                type     = "PROGRAM";
                            }
                            else if (trim.StartsWith("FUNCTION_BLOCK ", StringComparison.OrdinalIgnoreCase))
                            {
                                connType = ViConnType.Parse(trim.Substring(15), ViTypeCreation.None);
                                type     = "FUNCTION_BLOCK";
                            }
                            else if (trim.StartsWith("FUNCTION ", StringComparison.OrdinalIgnoreCase))
                            {
                                connType = ViConnType.Parse(trim.Substring(9), ViTypeCreation.Create);
                                type     = "FUNCTION";
                            }
                            else
                            {
                                alLines.Add(line);
                                continue;
                            }

                            if (connType == null)
                            {
                                return(false);
                            }
                            oldName = connType.Name;
                            // 名称一样,不需要修改
                            if (oldName == typeName)
                            {
                                return(true);
                            }

                            line = type + ' ' + typeName;
                            if (connType.Type != null)
                            {
                                line += " : " + connType.Type.Name;
                            }
                            alLines.Add(line);
                        }
                        else
                        {
                            alLines.Add(line);
                        }
                    }

                    // 没有读取到类型名称?
                    if (oldName == null)
                    {
                        return(false);
                    }
                }

                // 写入所有行
                using (StreamWriter writer = ViTextFile.CreateWriter(fileName))
                {
                    foreach (string line in alLines)
                    {
                        writer.WriteLine(line);
                    }
                }

                // 大功告成
                return(true);
            }
            catch (Exception ee)
            {
                Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message);
                Trace.WriteLine("### " + ee.StackTrace);
            }

            return(false);
        }