顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2012年9月28日 星期五

C# - Math.Round,四捨五入?

  










C# - Math.Round,好像是四捨六入呢?
要加上MidpointRounding.AwayFromZero才會真的四捨五入

2011年9月25日 星期日

ASP.NET Evaluator

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Evaluator 的摘要描述
///

public class Evaluator
{
    public static object Eval(string statement)
    {
        return _evaluatorType.InvokeMember("Eval", BindingFlags.InvokeMethod, null, _evaluator, new object[] { statement });
    }

    static Evaluator()
    {
        CodeDomProvider provider = CodeDomProvider.CreateProvider("JScript");
        CompilerParameters parameters;
        parameters = new CompilerParameters();
        parameters.GenerateInMemory = true;

        CompilerResults results;
        results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);

        Assembly assembly = results.CompiledAssembly;
        _evaluatorType = assembly.GetType("Evaluator");
        _evaluator = Activator.CreateInstance(_evaluatorType);
    }

    private static readonly object _evaluator;
    private static readonly Type _evaluatorType;
    private static readonly string _jscriptSource = @"class Evaluator { public function Eval(expr : String) : String { return eval(expr); } }";
}

2011年9月23日 星期五

C# Regular Expression



今天在利用 Regular Expression 取得網頁中的某些資料時,發現因為 pattern 都是包在 ( ) 內,如果Html 的內容也有 ( ) 符號,好像會比對不到資料

查了一下,可以用 Regex.Escape("(") 來將符號轉成字串查詢
這樣就可以取到 (02) 了

string pattern = Regex.Escape("(") + "(.*?)" + Regex.Escape(")");
string input = "(02)1234567";
MatchCollection matches = Regex.Matches(input, pattern);

foreach (Match match in matches)
{
MessageBox.Show(match.Value);
}


利用 C# 取得 SharePoint 網站集合的資料

1. 建立一個 C# 應用程式,
    NET Framework要用3.5
    要選擇 Any CPU
    要加入參考「Microsoft.SharePoint」


2. 輸入以下的程式碼

string targetSiteUrl = "";
using (SPSite siteCollection = new SPSite(targetSiteUrl))
{
SPWeb site = siteCollection.RootWeb;
MessageBox.Show(site.Title);

}




利用 C# 開啟 SharePoint 的 DashBoard 的功能

1. 開啟 Visual Studio 2010 ,建立一個 Windows Form 應用程式

2. 在畫面上佈置控制項如下

3. 要加入參考,「Microsoft.SharePoint」

4. 要選 NET Framework 3.5、平台要選 Any CPU


5. 貼上以下的程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;

namespace SPSDashBoard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SPWebService contentService = SPWebService.ContentService;
            SPDeveloperDashboardSettings developerDashboard =
              contentService.DeveloperDashboardSettings;
            developerDashboard.DisplayLevel = SPDeveloperDashboardLevel.On; //關閉是Off
            developerDashboard.Update();
            MessageBox.Show("Developer Dashboard updated.");

        }
    }
}


6. 回到網頁上,可以看到下方已經開啟 DashBoard了