Perfil de MohammadMohammad's spaceFotosBlogListasMais ![]() | Ajuda |
Mohammad's spaceC# 3.0 is the best |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Pastas públicas
|
17/10/2009 Talking about Visual Studio Beta 1 For Everyone
Quote Visual Studio Beta 1 For Everyone 5/12/2008 SurpriseI really surprised by new changes in Widows Live. In my i existing things are:
2/12/2008 My WebSite19/9/2008 Clear Your TracesNote: From now, all the code samples, can be dependent on the previous posts.Suppose that you want to clear all your traces in a computer, including:
The first one we want to clean is "Internet Cache". To do that, we must find its folder path using Environment.SpecialFolder.InternetCache. After that, by looping on each folder, we delete all of files. To make sure that no error will occurs (some files maybe in use), let's create a method, which deletes file in try..catch: 1: private static void DeleteFileSystem(string fileSystem) 2: {3: try 4: {5: Directory.Delete(fileSystem, true); 6: }7: catch 8: {9: try 10: { 11: File.Delete(fileSystem); 12: }13: catch 14: { 15: } 16: } 17: } 1: Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\Content.IE5\").ForEach(dir => Directory.GetFiles(dir).ForEach(DeleteFileSystem)); 1: Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\Content.IE5\").ForEach(DeleteFileSystem); 1: Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Recent)).ForEach(DeleteFileSystem);Internet URL History, Forms; "Run" dialog box history and recent programs in Start menu, are kept in Windows Registry. As before, let's create a method to delete registry keys in try...catch: 1: private static void DeleteRegKey(string regKey) 2: {3: try 4: { 5: Registry.CurrentUser.DeleteSubKeyTree(regKey); 6: }7: catch 8: { 9: } 10: }1: new string[] 2: {3: @"Software\Microsoft\Internet Explorer\IntelliForms" // Forms 4: , @"Software\Microsoft\Internet Explorer\TypedURLs" // Internet URL History 5: , @"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" //Run 6: , @"Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{75048700-EF1F-11D0-9888-006097DEACF9}\Count" //Recent Programs 7: , @"Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs" 8: }.ForEach(DeleteRegKey);It's almost done. Finally, we clean Recycle Bin. To do that, we must use an API: SHEmptyRecycleBin. So let's add an external method to our class, and decorate it with DllImport: 1: [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] 2: static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags); 1: try 2: {3: SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOCONFIRMATION | RecycleFlags.SHERB_NOPROGRESSUI | RecycleFlags.SHERB_NOSOUND); 4: }5: catch 6: { 7: }static class Program { [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags); static void Main(string[] args) { Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\Content.IE5\").ForEach(dir => Directory.GetFiles(dir).ForEach(DeleteFileSystem)); Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\Content.IE5\").ForEach(DeleteFileSystem);Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Recent)).ForEach(DeleteFileSystem); new string[] { @"Software\Microsoft\Internet Explorer\IntelliForms" // Forms , @"Software\Microsoft\Internet Explorer\TypedURLs" // Internet URL History , @"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" //Run , @"Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{75048700-EF1F-11D0-9888-006097DEACF9}\Count" //Recent Programs , @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU" //?? , @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU" //? , @"Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs", @"Software\Microsoft\Windows\CurrentVersion\Explorer\StreamMRU" }.ForEach(DeleteRegKey); try {uint result = SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOCONFIRMATION | RecycleFlags.SHERB_NOPROGRESSUI | RecycleFlags.SHERB_NOSOUND); } catch {} } private static void DeleteFileSystem(string fileSystem) { try { Directory.Delete(fileSystem, true);} catch { try {File.Delete(fileSystem); } catch {} } } private static void DeleteRegKey(string regKey) { try {Registry.CurrentUser.DeleteSubKeyTree(regKey); } catch {} } private static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> actor) {foreach (var item in source) actor(item); } } 11/9/2008 Some Extension Methods for "IEnumerable"sSystem.Linq.Enumerable has a lot of extension methods to make it easy to use the classes which are inherited from System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T>. To extend this class in my projects, I created a class with some other extension methods, named"Mohammad.Library.CoreLib.Helpers.EnumerableHelper". Here, I show you a set of these methods: 1: public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> actor) 2: {3: foreach (TSource item in source) 4: actor(item); 5: }6: public static void ForEach(this IEnumerable source, Action<Object> actor) 7: {8: foreach (Object item in source) 9: actor(item); 10: }11: public static void ForEach(this IDictionary source, Action<Object, Object> actor) 12: {13: foreach (object Key in source.Keys) 14: actor(Key, source[Key]); 15: }16: public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> source, Action<TKey, TValue> actor) 17: {18: foreach (TKey Key in source.Keys) 19: actor(Key, source[Key]); 20: }1: public static Collection<TSource> ToCollection<TSource>(this IEnumerable<TSource> source) 2: {3: Collection<TSource> result = new Collection<TSource>(); 4: source.ForEach(result.Add);5: return result; 6: } 7: 8: public static IEnumerable IfZero<TSource>(this IEnumerable source, IEnumerable defaultSource) 9: {10: return source.Cast<object>().Any() ? source : defaultSource; 11: }1: public static T Fold<T, U>(this IEnumerable<U> items, Func<T, U, T> func, T acc) 2: {3: foreach (var item in items) 4: acc = func(acc, item); 5: 6: return acc; 7: } 8: 9: public static T FoldLeft<T, U>(this IList<U> list, Func<T, U, T> func, T acc) 10: {11: for (int index = 0; index < list.Count; index++) 12: acc = func(acc, list[index]); 13: 14: return acc; 15: } 16: 17: public static T FoldRight<T, U>(this IList<U> list, Func<T, U, T> func, T acc) 18: {19: for (int index = list.Count - 1; index >= 0; index--) 20: acc = func(acc, list[index]); 21: 22: return acc; 23: } 24: 25: public static void ForIndex<T>(this IEnumerable<T> items, Action<int, T> action) 26: {27: int index = 0; 28: foreach (var item in items) 29: { 30: action(index, item); 31: index++; 32: } 33: }One Really Big Query Expression! 1: var pixelsQuery =2: from y in Enumerable.Range(0, screenHeight) 3: let recenterY = -(y - (screenHeight / 2.0)) / (2.0 * screenHeight)4: select from x in Enumerable.Range(0, screenWidth) 5: let recenterX = (x - (screenWidth / 2.0)) / (2.0 * screenWidth) 6: let point = Vector.Norm(Vector.Plus(scene.Camera.Forward, 7: Vector.Plus(Vector.Times(recenterX, scene.Camera.Right), 8: Vector.Times(recenterY, scene.Camera.Up))))9: let ray = new Ray { Start = scene.Camera.Pos, Dir = point } 10: let computeTraceRay = (Func<Func<TraceRayArgs, Color>, Func<TraceRayArgs, Color>>) 11: (f => traceRayArgs =>12: (from isect in 13: from thing in traceRayArgs.Scene.Things 14: select thing.Intersect(traceRayArgs.Ray)15: where isect != null 16: orderby isect.Dist 17: let d = isect.Ray.Dir 18: let pos = Vector.Plus(Vector.Times(isect.Dist, isect.Ray.Dir), isect.Ray.Start) 19: let normal = isect.Thing.Normal(pos) 20: let reflectDir = Vector.Minus(d, Vector.Times(2 * Vector.Dot(normal, d), normal))21: let naturalColors = from light in traceRayArgs.Scene.Lights 22: let ldis = Vector.Minus(light.Pos, pos) 23: let livec = Vector.Norm(ldis)24: let testRay = new Ray { Start = pos, Dir = livec } 25: let testIsects = from inter in 26: from thing in traceRayArgs.Scene.Things 27: select thing.Intersect(testRay)28: where inter != null 29: orderby inter.Dist 30: select inter 31: let testIsect = testIsects.FirstOrDefault()32: let neatIsect = testIsect == null ? 0 : testIsect.Dist 33: let isInShadow = !((neatIsect > Vector.Mag(ldis)) || (neatIsect == 0))34: where !isInShadow 35: let illum = Vector.Dot(livec, normal) 36: let lcolor = illum > 0 ? Color.Times(illum, light.Color) : Color.Make(0, 0, 0) 37: let specular = Vector.Dot(livec, Vector.Norm(reflectDir)) 38: let scolor = specular > 0 39: ? Color.Times(Math.Pow(specular, isect.Thing.Surface.Roughness), light.Color) 40: : Color.Make(0, 0, 0) 41: select Color.Plus(Color.Times(isect.Thing.Surface.Diffuse(pos), lcolor), 42: Color.Times(isect.Thing.Surface.Specular(pos), scolor)) 43: let reflectPos = Vector.Plus(pos, Vector.Times(.001, reflectDir)) 44: let reflectColor = traceRayArgs.Depth >= MaxDepth 45: ? Color.Make(.5, .5, .5) 46: : Color.Times(isect.Thing.Surface.Reflect(reflectPos), 47: f(new TraceRayArgs(new Ray { Start = reflectPos, Dir = reflectDir }, 48: traceRayArgs.Scene, 49: traceRayArgs.Depth + 1))) 50: select naturalColors.Aggregate(reflectColor, (color, natColor) => Color.Plus(color, natColor))) 51: .DefaultIfEmpty(Color.Background).First()) 52: let traceRay = Y(computeTraceRay)53: select new { X = x, Y = y, Color = traceRay(new TraceRayArgs(ray, scene, 0)) }; 54: 55: foreach (var row in pixelsQuery) 56: foreach (var pixel in row) 57: setPixel(pixel.X, pixel.Y, pixel.Color.ToDrawingColor());
Reference: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|