<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: File Access Exception in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11593642#M15304</link>
    <description>&lt;P&gt;Nope. Is it possible that something is incorrect in the file select method?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        /// &amp;lt;summary&amp;gt;
        /// Select a text file in the gioven folder.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="folder"&amp;gt;Initial folder.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="filename"&amp;gt;Selected filename on success.&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;Return true if a file was successfully selected.&amp;lt;/returns&amp;gt;
        static bool FileSelect(
          string folder,
          out string filename)
        {
                folder = Path.GetDirectoryName(folder);
            OpenFileDialog dlg = new OpenFileDialog
            {
                Title = "Select Schedule Data File",
                CheckFileExists = true,
                CheckPathExists = true,
                RestoreDirectory = true,
                InitialDirectory = folder,
                Filter = ".txt Files (*.txt)|*.txt"
            };
            bool rc = (DialogResult.OK == dlg.ShowDialog());
                filename = dlg.FileName;
                return rc;
        }&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 02 Dec 2022 15:32:13 GMT</pubDate>
    <dc:creator>Nathan.HilsonN67V6</dc:creator>
    <dc:date>2022-12-02T15:32:13Z</dc:date>
    <item>
      <title>File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11588897#M15296</link>
      <description>&lt;P&gt;Running into an issue where the txt file is unable to be accessed but i can still push things to the folder. I have admin rights on everything and pushed admin rights to the application. Not sure if it is code related.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;JtWindowHandle:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace ScheduleData
{
    /// &amp;lt;summary&amp;gt;
    /// Wrapper class for converting 
    /// IntPtr to IWin32Window.
    /// &amp;lt;/summary&amp;gt;
    public class JtWindowHandle : IWin32Window
    {
        IntPtr _hwnd;

        public JtWindowHandle(IntPtr h)
        {
            Debug.Assert(IntPtr.Zero != h,
              "expected non-null window handle");

            _hwnd = h;
        }

        public IntPtr Handle
        {
            get
            {
                return _hwnd;
            }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Command:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;#region Namespaces
using System.IO;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Autodesk;
using Autodesk.Revit;
using Autodesk.Revit.DB.Structure;
using TpMechanical.TpMechanical.Forms;
using System.Linq.Expressions;
#endregion

namespace ScheduleData
{
    
    [Transaction(TransactionMode.ReadOnly)]
    public class ScheduleDataCommand : IExternalCommand
    {
       
        /// &amp;lt;summary&amp;gt;
        /// Define the schedule export folder.
        /// All existing files will be overwritten.
        /// &amp;lt;/summary&amp;gt;
        const string _export_folder_name
          = "C:\\tmp\\schedule_data";

        /// &amp;lt;summary&amp;gt;
        /// Schedule export filename extension.
        /// &amp;lt;/summary&amp;gt;
        const string _ext = ".txt";

        const string _caption_prefix
          = "Schedule Data - ";

        /// &amp;lt;summary&amp;gt;
        /// Select a text file in the gioven folder.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="folder"&amp;gt;Initial folder.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="filename"&amp;gt;Selected filename on success.&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;Return true if a file was successfully selected.&amp;lt;/returns&amp;gt;
        static bool FileSelect(
          string folder,
          out string filename)
        {
                folder = Path.GetDirectoryName(folder);
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Title = "Select Schedule Data File";
                dlg.CheckFileExists = true;
                dlg.CheckPathExists = true;
                //dlg.RestoreDirectory = true;
                dlg.InitialDirectory = folder;
                dlg.Filter = ".txt Files (*.txt)|*.txt";
                bool rc = (DialogResult.OK == dlg.ShowDialog());
                filename = dlg.FileName;
                return rc;
        }
        void DisplayScheduleData(
          string filename,
          IWin32Window owner)
        {
            ScheduleDataParser parser
              = new ScheduleDataParser(filename);

            System.Windows.Forms.Form form
              = new System.Windows.Forms.Form();
            form.Size = new Size(400, 400);
            form.Text = _caption_prefix + parser.Name;

            DataGridView dg = new DataGridView();
            dg.AllowUserToAddRows = false;
            dg.AllowUserToDeleteRows = false;
            dg.AllowUserToOrderColumns = true;
            dg.Dock = System.Windows.Forms.DockStyle.Fill;
            dg.Location = new System.Drawing.Point(0, 0);
            dg.ReadOnly = true;
            dg.TabIndex = 0;
            dg.DataSource = parser.Table;
            dg.Parent = form;
            dg.IsAccessible= true;

            form.ShowDialog(owner);
        }

        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
                IWin32Window revit_window
                  = new JtWindowHandle(
                    Process.GetCurrentProcess().MainWindowHandle);

                UIApplication uiapp = commandData.Application;
                UIDocument uidoc = uiapp.ActiveUIDocument;
                Document doc = uidoc.Document;

                FilteredElementCollector col
                  = new FilteredElementCollector(doc)
                    .OfClass(typeof(ViewSchedule));

                ViewScheduleExportOptions opt
                  = new ViewScheduleExportOptions();

                    foreach (ViewSchedule vs in col)
                    {
                        Directory.CreateDirectory(
                         _export_folder_name);

                        vs.Export(_export_folder_name,
                        vs.Name + _ext, opt);
                    }    
                    
                string filename;

                while (FileSelect(_export_folder_name,
                  out filename))
                {
                    DisplayScheduleData(filename,
                      revit_window);
                }
                return Result.Succeeded;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DataParse:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace ScheduleData
{
    class ScheduleDataParser
    {
        /// &amp;lt;summary&amp;gt;
        /// Default schedule data file field delimiter.
        /// &amp;lt;/summary&amp;gt;
        static char[] _tabs = new char[] { '\t' };

        /// &amp;lt;summary&amp;gt;
        /// Strip the quotes around text strings 
        /// in the schedule data file.
        /// &amp;lt;/summary&amp;gt;
        static char[] _quotes = new char[] { '"' };

        string _name = null;
        DataTable _table = null;

        /// &amp;lt;summary&amp;gt;
        /// Schedule name
        /// &amp;lt;/summary&amp;gt;
        public string Name
        {
            get { return _name; }
        }

        /// &amp;lt;summary&amp;gt;
        /// Schedule columns and row data
        /// &amp;lt;/summary&amp;gt;
        public DataTable Table
        {
            get { return _table; }
        }

        public ScheduleDataParser(string filename)
        {
            StreamReader stream = File.OpenText(filename);

            string line;
            string[] a;

            while (null != (line = stream.ReadLine()))
            {
                a = line
                  .Split(_tabs)
                  .Select&amp;lt;string, string&amp;gt;(s =&amp;gt; s.Trim(_quotes))
                  .ToArray();

                // First line of text file contains 
                // schedule name

                if (null == _name)
                {
                    _name = a[0];
                    continue;
                }

                // Second line of text file contains 
                // schedule column names

                if (null == _table)
                {
                    _table = new DataTable();

                    foreach (string column_name in a)
                    {
                        DataColumn column = new DataColumn();
                        column.DataType = typeof(string);
                        column.ColumnName = column_name;
                        _table.Columns.Add(column);
                    }

                    _table.BeginLoadData();

                    continue;
                }

                // Remaining lines define schedula data

                DataRow dr = _table.LoadDataRow(a, true);
            }
            _table.EndLoadData();
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 19:40:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11588897#M15296</guid>
      <dc:creator>Nathan.HilsonN67V6</dc:creator>
      <dc:date>2022-11-30T19:40:39Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11589221#M15297</link>
      <description>&lt;P&gt;You are not closing the StreamReader.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Call 'Close' on it and also best to use a using statement, since you can as TextReader implements IDisposable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 21:59:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11589221#M15297</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2022-11-30T21:59:38Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11589439#M15298</link>
      <description>&lt;P&gt;No luck. Still Getting This error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NathanHilsonN67V6_0-1669853830937.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1146716i2642767C05E24AB7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="NathanHilsonN67V6_0-1669853830937.png" alt="NathanHilsonN67V6_0-1669853830937.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 00:17:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11589439#M15298</guid>
      <dc:creator>Nathan.HilsonN67V6</dc:creator>
      <dc:date>2022-12-01T00:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11589554#M15299</link>
      <description>&lt;P&gt;Only a few things I can suggest.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Check the folder is being created&lt;/P&gt;&lt;P&gt;Don't call Directory.Create repeatedly (create it once outside for each loop).&lt;/P&gt;&lt;P&gt;Try inputting path into export function with '\\' at end, some things are more pedantic than Windows in terms of distinguishing directories.&lt;/P&gt;&lt;P&gt;Try creating file manually in that location&lt;/P&gt;&lt;P&gt;It might also be the export function being called repeatedly, these things they hold onto resources, see if ViewSchedule.Dispose helps (although that is a long shot perhaps).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 01:51:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11589554#M15299</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2022-12-01T01:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11591205#M15300</link>
      <description>&lt;P&gt;Tried them all &amp;amp; still the same issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NathanHilsonN67V6_0-1669913789106.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1147022i7079AD82CD7F7667/image-size/medium?v=v2&amp;amp;px=400" role="button" title="NathanHilsonN67V6_0-1669913789106.png" alt="NathanHilsonN67V6_0-1669913789106.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is where I'm at currently. I moved the directory creation outside of the loop as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;                    foreach (ViewSchedule vs in col)
                    {
                            if (vs == null)
                            { 
                                continue; 
                            }
                            
                        vs.Export(_export_folder_name,
                        vs.Name + _ext, opt);  
                        vs.Dispose();
                
                    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 16:57:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11591205#M15300</guid>
      <dc:creator>Nathan.HilsonN67V6</dc:creator>
      <dc:date>2022-12-01T16:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11591277#M15301</link>
      <description>&lt;P&gt;Can you also confirm if it is happening on the first iteration of the loop i.e. is anything being created in the folder?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also how is the view schedule named?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I remember going back a few years I had a similar issue exporting dwgs that had shared images. Revit would hold onto the image so next time it was exported again it wouldn't overwrite.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only workaround was to create the file in a uniquely named folder and then copy it on elsewhere. Then try to look for and delete the mess left behind the next time the routine was run.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 17:46:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11591277#M15301</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2022-12-01T17:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11591732#M15302</link>
      <description>&lt;P&gt;Looks like the folder is being created &amp;amp; accessed which makes less sense to me if the resources aren't being held.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NathanHilsonN67V6_0-1669928111944.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1147127i8825CD6E6F1AE619/image-size/medium?v=v2&amp;amp;px=400" role="button" title="NathanHilsonN67V6_0-1669928111944.png" alt="NathanHilsonN67V6_0-1669928111944.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using the Revit SDK "Schedule Data" as a starting point &amp;amp; manipulating as needed if that helps as far as troubleshooting.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 20:57:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11591732#M15302</guid>
      <dc:creator>Nathan.HilsonN67V6</dc:creator>
      <dc:date>2022-12-01T20:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11592029#M15303</link>
      <description>&lt;P&gt;Try adding delay between exports to see if it is a latency issue or not.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 23:06:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11592029#M15303</guid>
      <dc:creator>RPTHOMAS108</dc:creator>
      <dc:date>2022-12-01T23:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11593642#M15304</link>
      <description>&lt;P&gt;Nope. Is it possible that something is incorrect in the file select method?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        /// &amp;lt;summary&amp;gt;
        /// Select a text file in the gioven folder.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="folder"&amp;gt;Initial folder.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="filename"&amp;gt;Selected filename on success.&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;Return true if a file was successfully selected.&amp;lt;/returns&amp;gt;
        static bool FileSelect(
          string folder,
          out string filename)
        {
                folder = Path.GetDirectoryName(folder);
            OpenFileDialog dlg = new OpenFileDialog
            {
                Title = "Select Schedule Data File",
                CheckFileExists = true,
                CheckPathExists = true,
                RestoreDirectory = true,
                InitialDirectory = folder,
                Filter = ".txt Files (*.txt)|*.txt"
            };
            bool rc = (DialogResult.OK == dlg.ShowDialog());
                filename = dlg.FileName;
                return rc;
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 02 Dec 2022 15:32:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11593642#M15304</guid>
      <dc:creator>Nathan.HilsonN67V6</dc:creator>
      <dc:date>2022-12-02T15:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: File Access Exception</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11611321#M15305</link>
      <description>&lt;P&gt;Got rid of the loop completely &amp;amp; it worked. You were right about it holding it. Thanks for the help!&lt;/P&gt;</description>
      <pubDate>Sat, 10 Dec 2022 16:40:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/file-access-exception/m-p/11611321#M15305</guid>
      <dc:creator>Nathan.HilsonN67V6</dc:creator>
      <dc:date>2022-12-10T16:40:53Z</dc:date>
    </item>
  </channel>
</rss>

