mailObj = new MailMessage("lsh2011@163.com", "JamesLi2015@hotmail.com","From Script.NET", "Body");
SMTPServer = new SmtpClient("smtp.163.com");
NTLMAuthentication = new System.Net.NetworkCredential("lsh2011@163.com", "password");
SMTPServer.UseDefaultCredentials = false;
SMTPServer.Credentials = NTLMAuthentication;
try
{
SMTPServer.Send(mailObj);
}
catch (ex)
{
Console.WriteLine(ex.Message);
}
finally
{
}
對於PDF文件的操作,選擇開源的iText library類庫。腳本代碼如下所示
//Create document at given location BeginDocument(“c:\\output.pdf”);//Change title in documentв’s meta dataВ ActiveDocument.AddTitle(‘Sample document’); //Create paragraphs with different alignment and color options Paragraph(‘Hello World’, ALIGN_CENTER); Paragraph(‘This is demo’, ALIGN_RIGHT, BLUE); Paragraph(‘This pdf was generated by S#’, GREEN); //Create a list of string items BeginList(); ListItem(‘One’); ListItem(‘Two’); ListItem(‘Three’); EndList(); //Create a table with tree columns BeginTable(3); //Create cells for the first row Cell(’1′); Cell(‘One’); Cell(Paragraph(‘Description of One’, RED)); //Create cells for second row Cell(’2′); Cell(‘Two’); Cell(‘Description of Two’); EndTable(); //Flush and close document EndDocument();
生成PDF的應用,它的原文是《Using S# to generate PDF documents》,請找到這篇文章並下載代碼體會。
通過這個例子,你也可以用它來生成Word/Excel文件。也許,一個動態生成文件的系統的方案產生於你的腦海中,根據用戶選擇的文件類型(PDF,DOC,XLS),動態調用這個腳本來生成相應類型的文件。這裡的動態生成是有好處的,你可以不用編譯程序,而只改變這裡的腳本代碼,來適應客戶對文件內容格式(比如layout)的更改。
來實現一個拷貝文件的copy命令,代碼如下
class Program
{
static void Main(string[] args)
{
RuntimeHost.Initialize();
Script script = Script.Compile(@"
return Copy('a.xls','d:\Document'); ");
script.Context.SetItem("Copy", new CopyFunction());
object result = script.Execute();
Console.WriteLine(result);
Console.ReadLine();
}
}
public class CopyFunction : IInvokable
{
public bool CanInvoke()
{
return true;
}
public object Invoke(IScriptContext context, object[] args)
{
string sourceFile =Convert.ToString(args[0]);
string destintionFolder=Convert.ToString(args[1]);
if(!Directory.Exists(destintionFolder))
Directory.CreateDirectory(destintionFolder);
string targetFile=Path.Combine(destintionFolder,Path.GetFileNameWithoutExtension( sourceFile)+Path.GetExtension( sourceFile));
File.Copy(sourceFile,targetFile);
return targetFile;
}
}
有了這個做基礎,你可以實現這樣的功能:每日構建 Daily Build,請參考文章《圖解持續集成--純命令行實現.Net項目每日構建》

也可以做到這樣的功能
![clip_image001[6] clip_image001[6]](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011316334014.jpg)
原文作者使用的bat/cmd的Windows外殼命令,而這裡使用的是Script.NET腳本,可以嵌入到其它應用程序中,被應用程序啟動並執行。
sql = DbProviderFactories.GetFactory("System.Data.SqlClient");
connection = sql.CreateConnection();
connection.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
connection.Open();
command = sql.CreateCommand();
command.Connection = connection;
command.CommandText = "select * from Customers";
reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["CompanyName"]+". "+ reader["ContactName"]);
}
connection.Dispose();
這個例子在前面已經舉例過,它可以引深為對其他數據源的操作(MySQL,Oracel…)
這個應用是我在思考工作流的規則編輯器時想到的,請看下圖
我們知道,在自定義的工作流系統中,CodeActivity是最有價值的活動,可以做任何想做的事情,但也非常不好用。因為工作流的用戶,不是做編程的,不懂C#.NET,所以,你不能指望他會改變這一點。Script.NET則彌補了這個缺陷,可以讓工作流設計人員,在我的腳本編輯環境中,編輯腳本,給工作流添加靈活的腳本代碼,在執行時,由Script.NET解析引擎執行。只要腳本編輯環境足夠智能靈活,提供的Script Sample足夠多,這個CodeActivity(應該改名叫ScriptActivity)為增加自定義的工作流系統的靈活性,發揮極大的作用。
要達到這個功能,要參考Windows Automation API,請參考這裡this article。
先來看一下應用的腳本是什麼樣子的,再來看實現原理
// Close existing instances of Notepad
Kill(“notepad”);
// Launch a new Notepad instance and get main window
window = Launch(“notepad”);
// Wait 1 second
Wait(1000);
// Get main editor region
edit = FindByClassName(window, “Edit”);
// focus main editor
FocusEditor(edit);
// Send sample text to the editor region
SendKeys.SendWait(“Automating Notepad using Windows UI Automation and S#”);
Wait(3000);
// Find [File] menu
mnuFile = FindById(window, “Item 1″);
// Expand [File] menu
Expand(mnuFile);
Wait(1000);
// Invoke [Save As] menu item
InvokeById(window, “Item 4″);
Wait(1000);
// Get [Save As] dialog
saveAsDialog = FindByName(window, “Save As”);
// Get access to [FileName] textbox
saveAsName = FindById(saveAsDialog, “1001″);
// Focus filename editor
FocusEditor(saveAsName);
// Write down file name
SendKeys.SendWait(“D:\\MyTextFile”);
// Send [Enter] keypress
SendKeys.SendWait(“{ENTER}”);
Wait(1000);
// Check whether Overwrite Dialog appeared
confirmSaveAs = FindByName(saveAsDialog, “Confirm Save As”);
if (confirmSaveAs != null)
{
// Click [OK] button
InvokeById(confirmSaveAs, “CommandButton_6″);
Wait(1000);
}
// Expand [File] menu
Expand(mnuFile);
Wait(1000);
// Click [Exit] item
InvokeById(window, “Item 7″);
這是在做什麼,打開Notepad,在裡面輸入文字,最後保存文件,全部的實現都是用腳本來做的。這令我想到了自動化測試,UI自動化測試。確實是這樣的,自動化的腳本代替了人工操作,完全不需要人為干預。
這個應用的原文是《Windows Automation: Automating Windows 7 Notepad within S# Script》。
對於Silverlight技術不熟悉,請用文章原文查看具體內容。我能理解到意思是,可以把以腳本的方式創建窗體,這樣的窗體是動態的,而不編譯時就寫死的,自然是非常靈活的方法。
推薦一個小技巧,我想把Script.NET的腳本,像對待外殼命令一樣,雙擊執行,或是右鍵點擊執行,如下面的效果所示

先把Script.NET的原代碼中的RunTests程序改造成可以接受一個文件參數的可執行文件,像這樣
它的原來的用法是這樣:Usage: RunTests.exe folderPath
改成這樣的用法 Usage: RunTests.exe scriptFile
這樣的用意是,讓它接受一個Script.NET腳本文件名,並能執行它。
再到外殼中注冊文件關聯,比如我把Script.NET的文件擴展名定義為spt文件,並添加這樣的注冊表項
Windows Registry Editor Version 5.00
[-HKEY_CLASSES_ROOT\SystemFileAssociations\.spt\shell\RunTests]
這樣就在外殼命令中關聯了Script.NET的腳本文件spt和它的執行程序RunTests,達到可以像bat文件一樣,雙擊執行。
總結:本質上,Script.NET的腳本運行還是解析為DotNet代碼的執行,所以不必懷疑它能做什麼,.NET能實現的功能,都能做到。問題是我們是否需要這樣的動態腳本,來增強程序的可擴展性,靈活性,這取決於你,it is up to you。