How to open an assembly in readonly using C#

How to open an assembly in readonly using C#

oransen
Collaborator Collaborator
1,879 Views
8 Replies
Message 1 of 9

How to open an assembly in readonly using C#

oransen
Collaborator
Collaborator

I'm able to open an assembly in C#, but I'd like to be able to open it in read only.

 

Is that possible? I've looked at the OpenWithOptions, which has visibility and NameValue list, but I can't see a read only option...is there one?

 

0 Likes
Accepted solutions (1)
1,880 Views
8 Replies
Replies (8)
Message 2 of 9

bradeneuropeArthur
Mentor
Mentor
Dim fio As New System.IO.FileInfo(fullfilename)

        fio.IsReadOnly = True

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 9

oransen
Collaborator
Collaborator

Hello, maybe I've missed something but you've told me how to open a file (not an assembly) in VB (not in C#).

 

Have I missed anything?

 

 

0 Likes
Message 4 of 9

bradeneuropeArthur
Mentor
Mentor

Hi,

 

No I did not open the file.

I created the read-only flag for the specific file.

After this you can open the file as usual.

The file will open read-only.

 

For C#:

 // Sets the read-only value of a file.
        public static void SetFileReadAccess(string FileName, bool SetReadOnly)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);

            // Set the IsReadOnly property.
            fInfo.IsReadOnly = SetReadOnly;

        }

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 5 of 9

oransen
Collaborator
Collaborator

So... I had missed something! Thanks for that, but is that the only way? It means  I have to reset the raw file properties at the end of the operation back to whatever they were...

0 Likes
Message 6 of 9

bradeneuropeArthur
Mentor
Mentor

only possibility is to open the file deferred!

 

This could also be an option.

 

But you asked for read-only specific.

 

Why do you like to open a file read-only?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 7 of 9

oransen
Collaborator
Collaborator

>> Why do you like to open a file read-only?

 

So neither my program nor my users can interfere with the assembly while my the program is running.

 

As far as the users are concerned I've realized that all I have to do is set it to be open invisible.

 

As far as my program is concerned I'll just have to be careful!

 

Thanks!

 

Owen

 

0 Likes
Message 8 of 9

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

If you turn defer updates on, Inventor blocks all kinds of editability, to the in-memory data.  But the file itself stays as is, so that is what I use for read-only processing, where I only want to read from the file, and not modify it.

 

Here is my complex open file tool that I use with many different scenarios (vb.net written in vs 2015-2019):

   Public Function OpenInventorFile(fileinfo As SIO.FileInfo, Optional Deferred As Boolean = False, Optional Hidden As Boolean = False, ByRef Optional FileOpened As Boolean = False) As Inventor.Document
        If fileinfo.Exists = False Then Return Nothing
        Dim invDoc As Inventor.Document = Nothing
        Dim invApp As Inventor.Application = TTSInventorTools.GetInventorApplication()
        Try
            If invApp IsNot Nothing Then
                'open document
                Try 'find open document
                    'invDoc = invApp.Documents.ItemByName(fileinfo.FullName)
                    For Each doc As Inventor.Document In invApp.Documents
                        If doc.FullFileName = fileinfo.FullName Then
                            invDoc = doc
                            Exit For
                        End If
                    Next
                    'invDoc.Save()
                Catch ex As Exception
                    'open file by name
                End Try
                If invDoc Is Nothing OrElse Hidden = False Then
                    If Deferred = True Then
                        Dim docOpenOptions As NameValueMap
                        docOpenOptions = invApp.TransientObjects.CreateNameValueMap
                        docOpenOptions.Add("SkipAllUnresolvedFiles", True)
                        If fileinfo.Extension = ".idw" Then docOpenOptions.Add("DeferUpdates", True)
                        Dim silentPrev As Boolean = invApp.SilentOperation
                        invApp.SilentOperation = True
                        If Hidden = True Then
                            invDoc = invApp.Documents.OpenWithOptions(fileinfo.FullName, docOpenOptions, False)
                            FileOpened = True
                        Else
                            invDoc = invApp.Documents.OpenWithOptions(fileinfo.FullName, docOpenOptions, True)
                            FileOpened = True
                        End If
                        invApp.SilentOperation = silentPrev
                    Else
                        Dim docOpenOptions As NameValueMap
                        docOpenOptions = invApp.TransientObjects.CreateNameValueMap
                        docOpenOptions.Add("SkipAllUnresolvedFiles", True)
                        If Hidden = True Then
                            invDoc = invApp.Documents.OpenWithOptions(fileinfo.FullName, docOpenOptions, False)
                            FileOpened = True
                        Else

                            invDoc = invApp.Documents.OpenWithOptions(fileinfo.FullName, docOpenOptions, True)
                            FileOpened = True
                        End If
                    End If
                End If
                If invDoc Is Nothing Then
                    FileOpened = False
                    Throw New Exception("Attempt to open file failed:" & fileinfo.FullName)
                End If
            End If
        Catch ex As Exception
            Dim eh As New ErrorHandler(New System.Exception("Can not open file:" & fileinfo.FullName & vbCr & ex.Message))
            eh.HandleIt()
        End Try
        Return invDoc
    End Function
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 9 of 9

oransen
Collaborator
Collaborator

Many thanks for that. 🙂

0 Likes