.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

File Search Path

5 REPLIES 5
Reply
Message 1 of 6
marcverdaasdonk9250
1026 Views, 5 Replies

File Search Path

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

Tags (3)
5 REPLIES 5
Message 2 of 6

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 6

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

Message 4 of 6
FritsA4
in reply to: Alexander.Rivilis

 

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;

 

 

 

 

 

 

 

 

Message 5 of 6

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

Message 6 of 6

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost