Get Current dynamo file path

I can’t take credit for this script, I only suggested a possible solution. Credits go to @Kulkul

All is good @4bimfercesp. Thank you.
I have added the 2 parentheses as a trial to solve the same error in that line 22. So, nothing changes if 1 parentheses or two exist in the routine. Thank you for your comments. :grinning:

Hi @Andreas_Dieckmann.
Thank you for that. I did use @KulKul’s script as well, but it does not display the entire file path, including server, as I need to display. Thank you for getting back to me. :grinning:

Just wondering if you guys found the answer. I found one working for me in the Designtech library:

2 Likes

Thanks. That one should be marked as the solution…

You could utilize this python code to get the file name, do note that this uses dynamorevit so may not work in dynamo sandbox or other dynamo hosted forms(formit, civil 3d).

3 Likes

import clr
clr.AddReference(‘DynamoServices’)
from Dynamo.Events import *
OUT = ExecutionEvents.ActiveSession.CurrentWorkspacePath

This may work

1 Like

Great call back. :slight_smile:

This API was added by the team shortly after the other method became the required fallback. Hopefully this will help users to find this more stable solution going forward!

2 Likes

This is update for Dynamo and Zerotouch Node :

Python Script :

import clr
clr.AddReference('DynamoServices')
from Dynamo.Events import *
OUT = ExecutionEvents.ActiveSession.CurrentWorkspacePath

Zero Touch Node :

using System.IO;
using Autodesk.DesignScript.Runtime;
using Dynamo.Applications
string fileName = DynamoRevit.RevitDynamoModel.CurrentWorkspace.FileName;

4 Likes

you can use the ExecutionEvents also from c# and it will be more portable than trying to access the DynamoModel directly.

2 Likes

Update for Zerotouch like @Michael_Kirschner2 recommend :

public static string FileFromRelativePath(string relativeFileName)
  {
      try
      {
          string fileName = Dynamo.Events.ExecutionEvents.ActiveSession.CurrentWorkspacePath;
          if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException(nameof(relativeFileName));
          var directory = Path.GetDirectoryName(fileName);
          if (directory == null) throw new ArgumentNullException(nameof(relativeFileName));
          string filePath = Path.Combine(directory, relativeFileName);
          if (!File.Exists(filePath)) throw new FileNotFoundException("File not found");
          return filePath;
      }
      catch (Exception)
      {
          return string.Empty;
      }
  }

Require Nuget :

<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="3.*">
            <ExcludeAssets>runtime</ExcludeAssets>
        </PackageReference>
1 Like