Posts
[C#][Extension Method]Get directory size
namespace ConsoleApplication8 { class Program { static void Main(string[] args) { GetFolderSize(@“C:\Program Files”); GetDirSize(@“C:\Program Files”); DirSize(new DirectoryInfo(@“C:\Program Files”));
var sw = Stopwatch.StartNew(); Console.WriteLine(GetFolderSize(@"C:\Program Files")); Console.WriteLine(sw.ElapsedMilliseconds); sw.Stop(); sw.Reset(); sw.Start(); Console.WriteLine(GetDirSize(@"C:\Program Files")); Console.WriteLine(sw.ElapsedMilliseconds); sw.Stop(); sw.Reset(); sw.Start(); Console.WriteLine(DirSize(new DirectoryInfo(@"C:\Program Files"))); Console.WriteLine(sw.ElapsedMilliseconds); } static long GetDirSize(string path) { return (from item in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) select new FileInfo(item).Length).Sum(); } static long GetFolderSize(string folder) // 取得資料夾大小 { return long.Parse((new Scripting.FileSystemObjectClass()).GetFolder(folder).Size.ToString()); } public static long DirSize(DirectoryInfo d) { long Size = 0; // Add file sizes.
read morePosts
[C#][Extension Method]String extension method(IsNull、IsNullOrEmpty、IsNullOrWhiteSpace、IsMatch)
namespace ConsoleApplication1 { public static class StringExtension { public static Boolean IsNull(this string str) { return str == null; }
public static Boolean IsNullOrEmpty(this string str) { return string.IsNullOrEmpty(str); } public static Boolean IsNullOrWhiteSpace(this string str) { return string.IsNullOrWhiteSpace(str); } public static bool IsMatch(this string str, string pattern) { if (str.IsNullOrEmpty()) throw new ArgumentNullException("str"); if (pattern.IsNullOrEmpty()) throw new ArgumentNullException("pattern"); return Regex.IsMatch(str, pattern); } } } namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string nullStr = null; string emptyStr = string.
read morePosts
[C#][JavaScript]WinForm與WebPage的JavaScript互通(一)
namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { webBrowser1.DocumentText = @"<script>function ShowAlert(alertMessage) {alert (alertMessage);}</script>"; } private void button1_Click(object sender, EventArgs e) { webBrowser1.Document.InvokeScript("ShowAlert", new object[] { "alert message..." }); } } }
namespace WindowsFormsApplication3 { [PermissionSet(SecurityAction.Demand, Name = “FullTrust”)] [ComVisible(true)] public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { webBrowser1.
read morePosts
[C#][JavaScript]WinForm與WebPage的JavaScript互通(二) - 動態加入並調用JavaScript
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(webBrowser1.Document.InvokeScript("GetVar", new object[] { "executeCount" }).ToString()); } public void OnWebPageReady() { HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement script = webBrowser1.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement)script.DomElement; element.text = "function GetVar(varName) { return eval('(' + varName + ')'); }"; head.AppendChild(script); } }</pre></div> public void OnWebPageReady() { webBrowser1.Document.InvokeScript("ShowAlert", new object[] { "WebPage Ready..." }); HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement script = webBrowser1.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement)script.DomElement; element.text = "function GetJsonValue(json, member) { return eval('(' + json + '.
read morePosts
[C#][VB.NET]Path.GetTempFileName的IOException
namespace ConsoleApplication9 { class Program { static void Main(string[] args) { var sw = new Stopwatch(); for (int i = 1; i <= 65536; ++i) { sw.Start(); Console.WriteLine(String.Format("{0} {1}", Path.GetTempFileName(), sw.ElapsedMilliseconds.ToString())); sw.Stop(); sw.Reset(); } } } }
read morePosts
[C#][VB.NET]使用MFT Scanner遍巡USN Journal,快速找出磁碟內的所有檔案
public class MFTScanner { private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); private const uint GENERIC_READ = 0x80000000; private const int FILE_SHARE_READ = 0x1; private const int FILE_SHARE_WRITE = 0x2; private const int OPEN_EXISTING = 3; private const int FILE_READ_ATTRIBUTES = 0x80; private const int FILE_NAME_IINFORMATION = 9; private const int FILE_FLAG_BACKUP_SEMANTICS = 0x2000000; private const int FILE_OPEN_FOR_BACKUP_INTENT = 0x4000; private const int FILE_OPEN_BY_FILE_ID = 0x2000; private const int FILE_OPEN = 0x1; private const int OBJ_CASE_INSENSITIVE = 0x40; private const int FSCTL_ENUM_USN_DATA = 0x900b3;
read morePosts
[C#][VB.NET]自定義例外對話框
<pre> ExceptionDialog.ShowBugWindowOnError()</pre> <pre class="alt"> <span class="kwrd">End</span> Sub</pre> <pre><span class="kwrd">Imports</span> System.Threading</pre> <pre class="alt"><span class="kwrd">Imports</span> System.Text</pre> <pre> </pre> <pre class="alt"><span class="kwrd">Public</span> <span class="kwrd">Class</span> ExceptionDialog</pre> <pre> </pre> <pre class="alt"><span class="preproc">#Region</span> <span class="str">"Const"</span></pre> <pre> <span class="kwrd">Const</span> OpenDetailButtonText <span class="kwrd">As</span> <span class="kwrd">String</span> = <span class="str">"v 詳細資料"</span></pre> <pre class="alt"> <span class="kwrd">Const</span> CloseDetailButtonText <span class="kwrd">As</span> <span class="kwrd">String</span> = <span class="str">"^ 詳細資料"</span></pre> <pre><span class="preproc">#End Region</span></pre> <pre class="alt"> </pre> <pre> </pre> <pre class="alt"> </pre> <pre><span class="preproc">#Region</span> <span class="str">"Var"</span></pre> <pre class="alt"> <span class="kwrd">Private</span> _isDetailOpened <span class="kwrd">As</span> <span class="kwrd">Boolean</span></pre> <pre><span class="preproc">#End Region</span></pre> <pre class="alt"> </pre> <pre> </pre> <pre class="alt"> </pre> <pre><span class="preproc">#Region</span> <span class="str">"Public Shared Method"</span></pre> <pre class="alt"> </pre> <pre> <span class="rem">'***************************************************************************</span></pre> <pre class="alt"> <span class="rem">'Author: Larry Nung</span></pre> <pre> <span class="rem">'Date: 2009/4/9</span></pre> <pre class="alt"> <span class="rem">'Purpose: </span></pre> <pre> <span class="rem">'Memo: </span></pre> <pre class="alt"> <span class="rem">'***************************************************************************</span></pre> <pre> <span class="rem">''' <summary></span></pre> <pre class="alt"> <span class="rem">''' Shows the bug window on error.
read morePosts
[C#]Command Line Parser Library
[OptionArray("o", "output", HelpText = "The output files to generate.")] public string[] OutputFiles { get; set; } [OptionList("k", "keywords", Separator = ':', HelpText = "Specify keywords to search the text, separated by a colon.")] public List<string> Keywords { get; set; } [ValueList(typeof(List<string>), MaximumElements = 3)] public List<string> Items { get; set; } ... }</pre></div> [HelpOption] public string GetUsage() { ... } }</pre></div> help.AddPreOptionsLine(" "); help.AddPreOptionsLine("CommandLine parser library demo..."); help.AddPreOptionsLine(" "); help.
read morePosts
[C#]DateTime 與 ISO8601 格式字串的相互轉換
return sucessed ? new DateTime?(dt) : null; }</pre></div>
read more