File Search Path

File Search Path

Anonymous
Not applicable
1,613 Views
5 Replies
Message 1 of 6

File Search Path

Anonymous
Not applicable

Hi,

 

I'm looking for some code that allows me to find files the same way AutoCAD does, by searching in the folders specified in the Support File Search Path.

 

 

Thanx,

Marc

0 Likes
1,614 Views
5 Replies
Replies (5)
Message 2 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

not tried, just searched through the object-catalog in vs ... and found:

Public MustOverride Function FindFile(fileName As String, database As Autodesk.AutoCAD.DatabaseServices.Database, hint As Autodesk.AutoCAD.DatabaseServices.FindFileHint) As String
Member von Autodesk.AutoCAD.DatabaseServices.HostApplicationServices

 

Sounds like that may be what you are searching for.

The other way would be using LISP   (findfile "myFileName.ext")

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 6

Alexander.Rivilis
Mentor
Mentor

Example: http://forums.autodesk.com/t5/NET/Files-in-support-file-paths-not-found/m-p/2557270#M15906

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 4 of 6

Anonymous
Not applicable

 

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;

 

 

 

 

 

 

 

 

0 Likes
Message 5 of 6

Anonymous
Not applicable

Thank you for this input, but do you have the VB code too?

 

Some extra info;

The goal for my code is, I need to eccess a textfile for read that is stored in one of the folders that are defined in the search paths.

 

 

Marc

0 Likes
Message 6 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> but do you have the VB code too?

What source code do you need more than the 3 answers above gave you? First you search the file, then (if it's found) you open it .... what is missing?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes