1. get the AcadSearchPath:
AcadPreferences acadPrefs = (AcadPreferences)Application.Preferences;
string supportPath = acadPrefs.Files.SupportPath;
//supportPath="path1;path2;path3..."
2.parse that string into an array of non empty pathnames
string[] searchPaths = supportPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray<string>();
3. sample 1 search for A folder
string menuPath = searchPaths .Where(name => name.ToUpper().Contains("TheFolderName")).First<string>();
Now the search for files
4. create the FindFile (overloaded) methodes, using the above searchPaths as a default
public string AcadFindFile(string fileToFind)
{
AcadPreferences acadPrefs = (AcadPreferences)Application.Preferences;
string[] pathsToSearchIn = acadPrefs.Files.SupportPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray<string>();
return AcadFindFile(fileToFind, pathsToSearchIn);
}
public string AcadFindFile(string fileToFind, string[] pathsToSearchIn)
{
string fileFound = "";
if (Path.IsPathRooted(fileToFind))
{
if (File.Exists(fileToFind))
return fileToFind;
fileToFind=Path.GetFileName(fileToFind);
}
var initialFolderList = pathsToSearchIn.ToArray<string>();
var existingFolderList = initialFolderList.Where(path => new DirectoryInfo(path).Exists);
foreach (var path in existingFolderList)
{
string fileFound2 = path..GetFiles(dir, fileToFind).First<string>();
}
var allfileList = existingFolderList.Select(dir => Directory.GetFiles(dir,fileToFind).First<string>());
var matchingfileList=allfileList.Where(x => x.ToString().ToLower().Contains(fileToFind.ToLower()));
var firstfile=matchingfileList.ToArray()[0];
var dirQuery = pathsToSearchIn.Where(dir => Directory.Exists(dir))
.Select(dir => Directory.GetFiles(dir).Where(x => x.ToLower().Contains(fileToFind.ToLower())));
//var filesQuery = InitialFolderList.Where(dir=> File.Exists(dir));
return fileFound;
}
5. usage of the AcadFindFile methodes
string theFile = AcadFindFile("lib_G-H_B.dwg");
if (theFile.Length>0 )
ed.WriteMessage("\n{0}", theFile);
you'll need the following:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;