Posts
[C#]Everything SDK
namespace Everything { /// <summary> /// /// </summary> public class EverythingAPI { #region Const const string EVERYTHING_DLL_NAME = “Everything.dll”; #endregion
#region DllImport [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_SetSearch(string lpSearchString); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SetMatchPath(bool bEnable); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SetMatchCase(bool bEnable); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SetMatchWholeWord(bool bEnable); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SetRegex(bool bEnable); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SetMax(int dwMax); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SetOffset(int dwOffset); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_GetMatchPath(); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_GetMatchCase(); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_GetMatchWholeWord(); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_GetRegex(); [DllImport(EVERYTHING_DLL_NAME)] private static extern UInt32 Everything_GetMax(); [DllImport(EVERYTHING_DLL_NAME)] private static extern UInt32 Everything_GetOffset(); [DllImport(EVERYTHING_DLL_NAME)] private static extern string Everything_GetSearch(); [DllImport(EVERYTHING_DLL_NAME)] private static extern StateCode Everything_GetLastError(); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_Query(); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_SortResultsByPath(); [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_GetNumFileResults(); [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_GetNumFolderResults(); [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_GetNumResults(); [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_GetTotFileResults(); [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_GetTotFolderResults(); [DllImport(EVERYTHING_DLL_NAME)] private static extern int Everything_GetTotResults(); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_IsVolumeResult(int nIndex); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_IsFolderResult(int nIndex); [DllImport(EVERYTHING_DLL_NAME)] private static extern bool Everything_IsFileResult(int nIndex); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_GetResultFullPathName(int nIndex, StringBuilder lpString, int nMaxCount); [DllImport(EVERYTHING_DLL_NAME)] private static extern void Everything_Reset(); #endregion #region Enum enum StateCode { OK, MemoryError, IPCError, RegisterClassExError, CreateWindowError, CreateThreadError, InvalidIndexError, InvalidCallError } #endregion #region Property /// <summary> /// Gets or sets a value indicating whether [match path].
read morePosts
[C#]More Effective C# 條款四十九: 考慮為大型物件使用弱引用
private static WeakReference m_ObjectCache { get { if (_objectCache == null) _objectCache = new WeakReference(null); return _objectCache; } } private static Object m_Cache { get { if (m_ObjectCache.Target == null) m_ObjectCache.Target = CreateBigObj(); return m_ObjectCache.Target; } } static void Main(string[] args) { Console.WriteLine(m_Cache); Console.WriteLine(m_Cache); GC.Collect(); Console.WriteLine(m_Cache); } ...</pre>
read morePosts
[C#]Effective C# 條款十七:盡量減少裝箱與拆箱
namespace ConsoleApplication18 { class Program { static void Main(string[] args) { Person p = new Person() { Name=“Larry”};
object o1 = (object)p; // boxing object o2 = p; // Implicit boxing ArrayList array = new ArrayList(); array.Add(p); //Implicit boxing //Unboxing Person p1 = (Person)o1; Person p2 = (Person)o2; p1.Name = "Larry1"; p2.Name = "Larry2"; Person p3 = (Person)array[0]; p3.Name = "Larry3"; Console.WriteLine(string.Format("p = {0}", p)); Console.WriteLine(string.Format("p1 = {0}", p1)); Console.
read morePosts
IEnumerable amp; IEnumerator介面的實作
PersonCollection persons = new PersonCollection ( new Person[]{new Person("Larry")}); foreach (Person person in persons) { Console.WriteLine(person.Name); } foreach (Person person in persons) { Console.WriteLine(person.Name); } } } public class Person { public string Name { get; set; } public Person(string name) { this.Name = name; } } public class PersonCollection : IEnumerable,IEnumerator { private Person[] _peoples; private int position = -1; public PersonCollection(Person[] list) { _peoples = list; } public bool MoveNext() { position++; return (position < _peoples.
read morePosts
[C#]Effective C# 條款十五:利用using和try/finally語句來清理資源.
conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Dispose(); }</pre> conn.Open(); cmd.ExecuteNonQuery(); } finally { if (cmd != null) cmd.Dispose(); if (conn != null) conn.Dispose(); } }</pre>
read morePosts
[C#]Effective C# 條款十四:利用建構子鏈
public string FirstName { get; set; } public string LastName { get; set; } public SexType Sex { get; set; } public int Age { get; set; } public string Common { get; set; } public Person(string firstName , string lastName) { this.FirstName = firstName; this.LastName = lastName; } public Person(string firstName, string lastName,int age) { this.FirstName = firstName; this.LastName = lastName; this.Age = age; } }</pre> public Person(string firstName, string lastName) { Init(firstName, lastName, 0); } public Person(string firstName, string lastName, int age) { Init(firstName, lastName, age); } private void Init(string firstName, string lastName, int age) { this.
read more