Author Topic: FindFile() | FilerError  (Read 3850 times)

0 Members and 1 Guest are viewing this topic.

BlackBox

  • King Gator
  • Posts: 3770
FindFile() | FilerError
« on: September 25, 2012, 10:33:38 AM »
I am iterating a list of strings (files), and using HostApplicationServices.Current.FindFile() within a try block to search for each respective file in SFSP. This results in an Autodesk.AutoCAD.Runtime.Exception if no file is found as Kean illustrates here.

Am I missing something, or shouldn't FindFile() simply return null if no file is found?  :?

"How we think determines what we do, and what we do determines what we get."

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: FindFile() | FilerError
« Reply #1 on: September 25, 2012, 11:17:58 AM »
I think its fairly common coding practice to only return valid results, throwing an error rather than returning a null which would need to be tested for.  That way, a try/catch can handle a number of different possible problems (including "Dave's not here, man...").
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: FindFile() | FilerError
« Reply #2 on: September 25, 2012, 12:48:49 PM »
RenderMan, looks like your running into the same problem I had with the property sets and AutoCAD API causing exceptions instead of returning results.

Why not use the .NET API System.IO.Directory or System.IO.File classes?
Revit 2019, AMEP 2019 64bit Win 10

BlackBox

  • King Gator
  • Posts: 3770
Re: FindFile() | FilerError
« Reply #3 on: September 25, 2012, 01:41:24 PM »
RenderMan, looks like your running into the same problem I had with the property sets and AutoCAD API causing exceptions instead of returning results.

As it happens, I wrote much of the OP in my last response to you in that thread, then felt that it would detract from your original topic (while loosely related), so I started a separate thread, given the Method specificity.

Why not use the .NET API System.IO.Directory or System.IO.File classes?

I'm searching the Support File Search Paths (SFSP) for a given Document.

Does System.IO offer such functionality for AutoCAD's SFSP, or would I have to parse, then iterate each path in SFSP + Application.GetSystemVariable("DWGPREFIX"), etc.?
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: FindFile() | FilerError
« Reply #4 on: September 25, 2012, 01:44:50 PM »
Also, if I can use System.IO for a FindFile() alternative, I am not opposed as I am already including calls to File.GetAttributes, File.SetAttributes, File.Exists, File.Delete, File.Move, etc..
"How we think determines what we do, and what we do determines what we get."

TheMaster

  • Guest
Re: FindFile() | FilerError
« Reply #5 on: September 25, 2012, 10:11:47 PM »
RenderMan, looks like your running into the same problem I had with the property sets and AutoCAD API causing exceptions instead of returning results.

As it happens, I wrote much of the OP in my last response to you in that thread, then felt that it would detract from your original topic (while loosely related), so I started a separate thread, given the Method specificity.

Why not use the .NET API System.IO.Directory or System.IO.File classes?

I'm searching the Support File Search Paths (SFSP) for a given Document.

Does System.IO offer such functionality for AutoCAD's SFSP, or would I have to parse, then iterate each path in SFSP + Application.GetSystemVariable("DWGPREFIX"), etc.?

It's probably easier to deal with the exception thrown by FindFile(), because I'm pretty sure that not all the paths it searches are actually included in DWGPREFIX.

BlackBox

  • King Gator
  • Posts: 3770
Re: FindFile() | FilerError
« Reply #6 on: September 25, 2012, 11:25:51 PM »
As I understand it, DwgPrefix is searched before the first path in SFSP; which is the premis for how that Acad.lsp[fas] virus perpetuates.

Regarding the exception, I appreciate your clarifying... This is just something I'm now making peace with as I transition from using Nil with LISP, into .NET development. Each has their place, and with time I will better be able to negotiate logic and necessary exception handling.

Cheers! :beer:
"How we think determines what we do, and what we do determines what we get."

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: FindFile() | FilerError
« Reply #7 on: September 26, 2012, 07:53:22 AM »
Quote
I'm searching the Support File Search Paths (SFSP) for a given Document.

Does System.IO offer such functionality for AutoCAD's SFSP, or would I have to parse, then iterate each path in SFSP + Application.GetSystemVariable("DWGPREFIX"), etc.?

No, you would have to get the paths from AutoCAD and then retrieve all the filenames with System.IO.Directory.  It would add a couple of extra steps but it would avoid exception trapping if that bothers you.
Revit 2019, AMEP 2019 64bit Win 10

BlackBox

  • King Gator
  • Posts: 3770
Re: FindFile() | FilerError
« Reply #8 on: September 26, 2012, 09:57:30 AM »
Quote
I'm searching the Support File Search Paths (SFSP) for a given Document.

Does System.IO offer such functionality for AutoCAD's SFSP, or would I have to parse, then iterate each path in SFSP + Application.GetSystemVariable("DWGPREFIX"), etc.?

No, you would have to get the paths from AutoCAD and then retrieve all the filenames with System.IO.Directory.  It would add a couple of extra steps but it would avoid exception trapping if that bothers you.

No worries; I think this is just a bit of growing pains on my part as I learn a higher level language. I'm beginning to understand that there's more out there than Nil:-D

FWIW - I've actually done this before (iterating SFSP in lieu of FindFile), in this old post... I had (unsuccessfully) tried to employ FindFile first and wasn't getting the desired results, but Gile got me squared away.
« Last Edit: September 26, 2012, 10:02:57 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."