Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the "dir" (aka "Get-ChildItem") powershell cmdlet from C#.<br> (I'm not saying whether you should.) </p> <p>You have to add this reference to your project file ("<em>.csproj" or "</em>.vcproj") manually: </p> <pre><code>&lt;Reference Include="System.Management.Automation" /&gt; </code></pre> <p>See here for more details on how to use cmdlets from C#: <a href="http://www.devx.com/tips/Tip/42716" rel="nofollow">http://www.devx.com/tips/Tip/42716</a> </p> <p>Here a working program: </p> <pre><code>using System; using System.Collections.Generic; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Collections.ObjectModel; namespace CsWildcard { class Program { static IEnumerable&lt;string&gt; CmdletDirGlobbing(string basePath, string glob){ Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); // cd to basePath if(basePath != null){ Pipeline cdPipeline = runspace.CreatePipeline(); Command cdCommand = new Command("cd"); cdCommand.Parameters.Add("Path", basePath); cdPipeline.Commands.Add(cdCommand); cdPipeline.Invoke(); // run the cmdlet } // run the "dir" cmdlet (e.g. "dir C:\*\*\*.txt" ) Pipeline dirPipeline = runspace.CreatePipeline(); Command dirCommand = new Command("dir"); dirCommand.Parameters.Add("Path", glob); dirPipeline.Commands.Add(dirCommand); Collection&lt;PSObject&gt; dirOutput = dirPipeline.Invoke(); // for each found file foreach (PSObject psObject in dirOutput) { PSMemberInfoCollection&lt;PSPropertyInfo&gt; a = psObject.Properties; // look for the full path ("FullName") foreach (PSPropertyInfo psPropertyInfo in psObject.Properties) { if (psPropertyInfo.Name == "FullName") { yield return psPropertyInfo.Value.ToString(); // yield it } } } } static void Main(string[] args) { foreach(string path in CmdletDirGlobbing(null,"C:\\*\\*\\*.txt")){ System.Console.WriteLine(path); } foreach (string path in CmdletDirGlobbing("C:\\", "*\\*\\*.exe")) { System.Console.WriteLine(path); } Console.ReadKey(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload