当前位置: 首页>>代码示例>>C#>>正文


C# StringBuilder.WriteToFile方法代码示例

本文整理汇总了C#中StringBuilder.WriteToFile方法的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.WriteToFile方法的具体用法?C# StringBuilder.WriteToFile怎么用?C# StringBuilder.WriteToFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StringBuilder的用法示例。


在下文中一共展示了StringBuilder.WriteToFile方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SavePackageId

    public static void SavePackageId(Template t, string outDir)
    {
        var allpkgs = t.Structs.Where(a => a.IsPackage).OrderByDescending(a => a.RefCounter);

        // 生成 packageId -- ProjectName + Namespace + Name 的 csv 文件
        // 根据 接收者生成 pkgId 的规则,记录接收者的 ProjectName.
        var sb = new StringBuilder();
        var lst = new List<string>();
        foreach (var pkg in t.Structs.Where(a => a.IsPackage).OrderByDescending(a => a.RefCounter))
        {
            var pn = "";
            if (pkg.Projects.Count > 0)
            {
                var o = pkg.Projects.Find(a => a.SendRecvType == TemplateLibrary.SendRecvTypes.SendAndRecv
                || a.SendRecvType == TemplateLibrary.SendRecvTypes.RecvOnly);
                if (o != null)
                {
                    pn = o.Project.Name;
                }
            }
            lst.Add(pkg.PackageId + "," + pn + "," + pkg.Namespace + "," + pkg.Name);
        }
        lst.Sort((a, b) =>
        {
            var aa = a.Split(',');
            var bb = b.Split(',');
            return long.Parse(aa[0]).CompareTo(long.Parse(bb[0]));
        });
        sb.Append(string.Join(Environment.NewLine, lst));


        sb.WriteToFile(Path.Combine(outDir, t.Name + "_packageId_config.csv"));
    }
开发者ID:denghe,项目名称:xxpkggen,代码行数:33,代码来源:LoadSaves.cs

示例2: Gen

        public static string Gen( Template sd, string outputDirPath, string fn )
        {
            var sb = new StringBuilder();

            try {
                sb.WriteToFile( Path.Combine( outputDirPath, fn + "_packets.lua" ), false );
            }
            catch( Exception ex ) {
                return ex.Message;
            }

            return "";
        }
开发者ID:whuthj,项目名称:69net,代码行数:13,代码来源:GenEmpty.cs

示例3: ExtensionMethods_WriteToFile_WhenSavingStringBuilder_ExpectSavedFile

        public void ExtensionMethods_WriteToFile_WhenSavingStringBuilder_ExpectSavedFile()
        {
            //------------Setup for test--------------------------
            var tmpFile = Path.GetTempFileName();
            const string val = "<x><y>1</y></x>";
            StringBuilder value = new StringBuilder(val);

            //------------Execute Test---------------------------
            value.WriteToFile(tmpFile, Encoding.UTF8,new TxFileManager());

            //------------Assert Results-------------------------
            var result = File.ReadAllText(tmpFile);

            // clean up ;)
            File.Delete(tmpFile);

            Assert.AreEqual(val, result, "WriteToFile did not write");
        }
开发者ID:NatashaSchutte,项目名称:Warewolf-ESB,代码行数:18,代码来源:ExtensionMethodsTest.cs

示例4: ExtensionMethods_WriteToFile_RollbackIfError

        public void ExtensionMethods_WriteToFile_RollbackIfError()
        {
            //------------Setup for test--------------------------
            var tmpFile = Path.GetTempFileName();
            File.WriteAllText(tmpFile, "this is going to be some very long test just to ensure we can over write it");
            const string val = "<x><y>1</y></x>";
            StringBuilder value = new StringBuilder(val);

            //------------Execute Test---------------------------
            IFileManager fm = new TxFileManager();
            value.WriteToFile(tmpFile, Encoding.UTF8, fm);

            //------------Assert Results-------------------------
            var result = File.ReadAllText(tmpFile);

            // clean up ;)
            File.Delete(tmpFile);

            Assert.AreEqual(val, result, "WriteToFile did not truncate");
        }
开发者ID:NatashaSchutte,项目名称:Warewolf-ESB,代码行数:20,代码来源:ExtensionMethodsTest.cs

示例5: Gen


//.........这里部分代码省略.........
            _" + f.Name.ToFirstLower() + @" = v;
            }" );
                            }
                            else
                            {
                                sb.Append( @"
            inline void set" + f.Name.ToFirstUpper() + @"( " + tn + @" v )
            {
            _" + f.Name.ToFirstLower() + @" = v;
            }" );
                            }
                        }
                        else
                        {
                            sb.Append( @"
            template<typename VT> void set" + f.Name.ToFirstUpper() + @"( VT&& v )
            {
            _" + f.Name.ToFirstLower() + @" = std::forward<VT>( v );
            }" );
                        }
                    }
                }
                sb.Append( @"
            };" );
            }

            sb.Append( @"
            }
            #endif
            " );

            try
            {
                sb.WriteToFile( Path.Combine( outDir, ns + ".h" ) );
            }
            catch( Exception ex )
            {
                return ex.Message;
            }

            sb.Clear();

            sb.Append( @"#include ""Lib/All.h""
            #include """ + ns + @".h""

            namespace " + pn + @"Packets
            {" );
            foreach( var c in t.Classes )
            {
                sb.Append( @"
            ushort " + c.Name + @"::getTypeID()
            {
            return " + c.TypeID + @";
            }" );

                if( !IsPod( c ) )
                {
                    sb.Append( @"
            " + c.Name + @"::" + c.Name + @"( " + c.Name + @" const& other )" );
                    bool isFirst = true;
                    foreach( var f in c.Fields )
                    {
                        sb.Append( @"
            " + ( isFirst ? ":" : "," ) + " _" + f.Name.ToFirstLower() + @"( other._" + f.Name.ToFirstLower() + @" )" );
                        isFirst = false;
                    }
开发者ID:whuthj,项目名称:69net,代码行数:67,代码来源:GenCPP.cs

示例6: Gen


//.........这里部分代码省略.........
                //    for (uint32_t i = 0; i < count; i++)
                //    {
                //        if (rtv = VarReadFrom(srcBuf, dataLen, offset, idx)) return rtv;
                //        switch (idx)
                //        {");
                //            foreach (var f in c.Members.OrderBy(a => a.Ordinal))
                //            {
                //                sb.Append(@"
                //        case " + f.Ordinal + @":
                //        {");
                //                if (f.Deprecated != null)
                //                {
                //                    var tn = f.Type.IsArray ? GetTypeKeyword(f.Type.ChildType) : GetTypeKeyword(f);
                //                    sb.Append(@"
                //            " + tn + " tmp;");
                //                }
                //                sb.Append(@"
                //            if (rtv = " + (f.Compress && f.Type.Compressable() ? "Var" : "") + "ReadFrom" + ((f.Type.IsContainer && !f.Type.IsArray) ? "Limit" : "") + "(srcBuf, dataLen, offset");
                //                if (f.Type.IsContainer && !f.Type.IsArray)
                //                {
                //                    sb.Append(@", " + f.MinLen + ", " + f.MaxLen);
                //                }
                //                sb.Append(", " + (f.Deprecated != null ? "tmp" : ("out." + f.Name)) + @")) return rtv;
                //            break;
                //        }");
                //            }
                //            sb.Append(@"
                //        default:
                //            offset = bak + len;
                //            return rtv;
                //        }
                //    }
                //    return rtv;
                //}");
            }
            else
            {

                sb.Append(@"
    inline size_t Calc(" + ctn + @" const &in)
    {
        return ");
                if (c.Members.Count == 0)
                {
                    sb.Append(@"0");
                }
                else
                {
                    sb.Append(@"Calc(");
                    foreach (var f in c.Members)
                    {
                        sb.Append(@"in." + f.Name + ", ");
                    }
                    sb.Length -= 2;
                    sb.Append(")");
                }
                sb.Append(@";
    }
    inline size_t WriteTo(char *dstBuf, " + ctn + @" const &in)
    {
        size_t len = 0;");
                foreach (var f in c.Members)
                {
                    sb.Append(@"
        len += " + (f.Compress && f.Type.Compressable() ? "Var" : "") + "WriteTo(dstBuf + len, in." + f.Name + ");");
                }
                sb.Append(@"
        return len;
    }
    template<>
    inline int32_t ReadFrom(char const *srcBuf, size_t const &dataLen, size_t &offset, " + ctn + @" &out)
    {
        int32_t rtv = 0;");
                foreach (var f in c.Members)
                {
                    sb.Append(@"
        if (rtv = " + (f.Compress && f.Type.Compressable() ? "Var" : "") + "ReadFrom" + ((f.Type.IsContainer && !f.Type.IsArray) ? "Limit" : "") + "(srcBuf, dataLen, offset");
                    if (f.Type.IsContainer && !f.Type.IsArray)
                    {
                        sb.Append(@", " + f.MinLen + ", " + f.MaxLen);
                    }
                    sb.Append(", out." + f.Name + @")) return rtv;");
                }
                sb.Append(@"
        return rtv;
    }");

            }
        }
        sb.Append(@"
}
}");



        sb.WriteToFile(Path.Combine(outDir, pn + ".h"));

        #endregion

    }
开发者ID:denghe,项目名称:xxpkggen,代码行数:101,代码来源:GenCPP.cs

示例7: Gen


//.........这里部分代码省略.........
            foreach( var ns_ in t.Namespaces )
            {
                var ns = ( ns_ != "" ? ns_ : pn );
                foreach( var c in t.Classes.Where( a => a.Namespace == ns_ ) )
                {
                    var en = ns.Replace( ".", "_" ) + "_" + c.Name;
                    sb.Append( @"
            if( typeof( T ) == typeof( " + ns + "." + c.Name + @" ) ) On_" + en + @" += func;" );
                }
            }
            sb.Append(@"
            }
            private void UnegisterHandler<T>( dynamic func ) where T : ISC
            {" );
            foreach( var ns_ in t.Namespaces )
            {
                var ns = ( ns_ != "" ? ns_ : pn );
                foreach( var c in t.Classes.Where( a => a.Namespace == ns_ ) )
                {
                    var en = ns.Replace( ".", "_" ) + "_" + c.Name;
                    sb.Append( @"
            if( typeof( T ) == typeof( " + ns + "." + c.Name + @" ) ) On_" + en + @" -= func;" );
                }
            }
            sb.Append( @"
            }

            #endregion

            " );

            try
            {
                sb.WriteToFile( Path.Combine( outputDirPath, pn + "_Samples.txt" ) );
            }
            catch( Exception ex )
            {
                return ex.Message;
            }

            sb.Clear();
            sb.Append( @"using SerialHelper;

            public static partial class " + pn + @"_AllClassHandler
            {
            public static ISC read( int opcode, byte[] buf )
            {
            var b = new Bytes( buf, buf.Length );
            switch( (" + pn + @"_AllClassEnums)opcode )
            {" );
            foreach( var ns_ in t.Namespaces )
            {
                var ns = ( ns_ != "" ? ns_ : pn );
                foreach( var c in t.Classes.Where( a => a.Namespace == ns_ ) )
                {
                    var en = ns.Replace( ".", "_" ) + "_" + c.Name;

                    sb.Append( @"
            case " + pn + @"_AllClassEnums." + en + @":
            {
                var o = new " + ns + @"." + c.Name + @"();
                var rtv = o.readFrom( b );
                if( rtv != ReadState.Success ) return null;
                return o;
            }" );
                }
开发者ID:whuthj,项目名称:69net,代码行数:67,代码来源:GenCS.cs

示例8: SaveMemberOrdinal

    public static void SaveMemberOrdinal(Template t, string outDir)
    {
        var allclass = t.Structs;

        // 生成 Namespace, Class Name, Property Name, property id 的 csv 文件
        var sb = new StringBuilder();
        foreach (var c in allclass)
        {
            foreach (var m in c.Members)
            {
                sb.Append(c.Namespace + "," + c.Name + "," + m.Name + "," + m.Ordinal + "\n");
            }
        }
        if (sb.Length > 0)
        {
            --sb.Length; // 去掉最后 \n
        }
        sb.WriteToFile(Path.Combine(outDir, t.Name + "_memberOrdinal_config.csv"));
    }
开发者ID:denghe,项目名称:xxpkggen,代码行数:19,代码来源:LoadSaves.cs


注:本文中的StringBuilder.WriteToFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。