Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sep 15, 2009

Change Current Directory To Execute External EXE File

Sometimes you need to trigger an exe file not within your current application directory.
You may change your current directory ( one of the easiest method ).
Yet you have to change back your directory after you done the execution process.

//Set the current directory.
Directory.SetCurrentDirectory(my-dir-variable);

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = my-file-name-variable;
 
Process.Start(startInfo);

Bookmark and Share

Aug 19, 2009

Get Files From SubFolder Recursively

Get files from directory (including all subdirectories)

If you want to search also in subfolders use parameter SearchOption.A­llDirectories.

string[] filePaths = Directory.GetFiles(@"c:\test\", "*.bmp",
SearchOption.AllDirectories);

returns:
"c:\test\test.BMP"
"c:\test\test2\test2.BMP"

for Folder:

string[] folders = System.IO.Directory.GetDirectories(@"C:\test\","*", System.IO.SearchOption.AllDirectories);

Bookmark and Share

Mar 15, 2009

C# List.Find() List.Exist() and Predicates

Example 1:

List myObjects = new List();

SomeObject desiredObject =
myObjects.Find(delegate(SomeObject o) { return o.Id == desiredId; });


Example 2:
Source from
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

using System;
using System.Collections.Generic;

public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();

dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}

Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs.TrueForAll(EndsWithSaurus));

Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));

Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs.FindLast(EndsWithSaurus));

Console.WriteLine("\nFindAll(EndsWithSaurus):");
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);

foreach(string dinosaur in sublist)
{
Console.WriteLine(dinosaur);
}

Console.WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs.RemoveAll(EndsWithSaurus));

Console.WriteLine("\nList now contains:");
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}

Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists(EndsWithSaurus));
}

// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
}

/* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

TrueForAll(EndsWithSaurus): False

Find(EndsWithSaurus): Amargasaurus

FindLast(EndsWithSaurus): Dilophosaurus

FindAll(EndsWithSaurus):
Amargasaurus
Dilophosaurus

2 elements removed by RemoveAll(EndsWithSaurus).

List now contains:
Compsognathus
Oviraptor
Velociraptor
Deinonychus
Gallimimus
Triceratops

Exists(EndsWithSaurus): False
*/