.NET

Reply
Member
marcverdaasdonk9250
Posts: 4
Registered: ‎07-28-2010
Message 1 of 6 (429 Views)

File Search Path

429 Views, 5 Replies
12-08-2011 04:14 AM

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

*Expert Elite*
Alfred.NESWADBA
Posts: 9,583
Registered: ‎06-29-2007
Message 2 of 6 (426 Views)

Re: File Search Path

12-08-2011 04:20 AM in reply to: marcverdaasdonk9250

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
-------------------------------------------------------------------------
Moderator
Alexander.Rivilis
Posts: 1,452
Registered: ‎04-09-2008
Message 3 of 6 (416 Views)

Re: File Search Path

12-08-2011 06:18 AM in reply to: marcverdaasdonk9250

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


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
FritsA4
Posts: 26
Registered: ‎09-15-2010
Message 4 of 6 (404 Views)

Re: File Search Path

12-08-2011 12:13 PM 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;

 

 

 

 

 

 

 

 

Member
marcverdaasdonk9250
Posts: 4
Registered: ‎07-28-2010
Message 5 of 6 (370 Views)

Re: File Search Path

12-12-2011 04:35 AM in reply to: FritsA4

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

*Expert Elite*
Alfred.NESWADBA
Posts: 9,583
Registered: ‎06-29-2007
Message 6 of 6 (367 Views)

Re: File Search Path

12-12-2011 05:40 AM in reply to: marcverdaasdonk9250

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
-------------------------------------------------------------------------
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.