在c#使用"Environment.NewLine"來取代vb的"vbCrLf"
最近小舖有人提到這個問題....
其實一般在網路上找到的答案都是用"\r\n" 來取代 "vbCrLf"
但有一個更好的方法就是用c#本身就有的"Environment.NewLine"來取代vb的"vbCrLf"
小弟介紹一個範例用"Environment.NewLine"與"\r\n"都可達到vb的"vbCrLf"功能
c#範例
newline.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="newline.aspx.cs" Inherits="newline" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>newline</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server" Height="100px" TextMode="MultiLine" Width="300px"></asp:TextBox></div>
12 </form>
13 </body>
14 </html>
15
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>newline</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server" Height="100px" TextMode="MultiLine" Width="300px"></asp:TextBox></div>
12 </form>
13 </body>
14 </html>
15
newline.aspx.cs
01
using System;
02
using System.Data;
03
using System.Configuration;
04
using System.Collections;
05
using System.Web;
06
using System.Web.Security;
07
using System.Web.UI;
08
using System.Web.UI.WebControls;
09
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class newline : System.Web.UI.Page
13
{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
//使用"Environment.NewLine"與"\r\n"都可以達到vb.net的"vbCrLf"功能
17
this.TextBox1.Text = "F6 Team" + Environment.NewLine + "puma" + "\r\n" + "blueshop";
18
}
19
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

執行結果: