Results 1 to 7 of 7

Thread: Open a file as read only

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved Open a file as read only

    Using the following :
    System.Diagnostics.Process.Start("YourFile.txt");

    I can open a file with whatever application is suitable for that file type if supported but what I want to know is.

    Can i open that file in read only mode ?
    I would prefer not to make a reference to the various COM objects for office applications.
    Last edited by venerable bede; Aug 7th, 2007 at 06:50 AM.

    Parksie

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open a file as read only

    Where does Office come into this?

    If you use System.IO class you can open a file for read/write operations using the ReadOnly file mode.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Open a file as read only

    Open it yourself and lock it, then open it in the 3rd party application; or, if the application has a command-line switch to control read-only mode, start the application with that switch and pass the file name as an argument.

  4. #4

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Open a file as read only

    Cheers.
    Is it possible to get a handle on the application closing ?
    I don't think this is possible but it would be useful if I my application could be alerted when word,notepad or whatever closed so I could run some clean up code.

    Parksie

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Open a file as read only

    Yes:
    VB.NET Code:
    1. Imports System.Diagnostics
    2. ' ...
    3. Dim notepad As Process = Process.Start("notepad")
    4. notepad.EnableRaisingEvents = True
    5. AddHandler notepad.Exited, AddressOf notepad_Exited
    6. ' ...
    7. Private Sub notepad_exited(sender As Object, e As EventArgs)
    8.   MessageBox.Show("Notepad was closed")
    9. End Sub

  6. #6

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved Re: Open a file as read only

    Thanks penegate.

    You are a god among men

    Parksie

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Open a file as read only

    I like to think so, but it's nice to hear it from someone else.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width