ezG
ezG

Reputation: 421

Cannot detect READ-ONLY state of active document

Looking for a C# solution I can use through VSTO. We're running Windows 10 and Office 365

When we open a PowerPoint file from SharePoint, we get the following alert.
READ-ONLY Message

If you were to check the ReadOnly property of the Presentation object, it would have a value of false

The problem is that we have code that executes based on whether or not the ReadOnly property of the active Presentation is true or false.

My guess is that this ReadOnly state is happening in a different window similar to the ProtectedViewWindow

How can I programmatically check for the READ-ONLY shown in the above image?

Upvotes: 1

Views: 90

Answers (2)

mandypea
mandypea

Reputation: 63

If you have Windows Subsystem for Linux you can 'programmatically' check if a file is readonly by looking at the file's permissions displayed by the ls -la command. If the file is read-only, then the permissions will be -r--r--r-- or -r--------.

The first set of permissions (-) indicates the file type (d for directory). The next set of three (r--) indicates the read permissions for the owner of the file, the second set (r--) indicates the read permissions for the group, and the third set (r--) indicates the read permissions for others.

If there is no w in any of the three sets of permissions, it means the file is read-only. If there is a w in any of the sets of permissions, it means the file is not read-only and can be modified.

You could use this command in a bash script programmatically.

In C#:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // Prompt the user to enter the file path
        Console.Write("Enter the file path: ");
        string filePath = Console.ReadLine();

        // Check if the file is read-only
        if (File.Exists(filePath) && (File.GetAttributes(filePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            Console.WriteLine("{0} is read-only.", filePath);
        }
        else
        {
            Console.WriteLine("{0} is not read-only.", filePath);
        }
    }
}

Here's how the code works:

  1. The code prompts the user to enter the file path. You can also statically set that as avariable.

  2. The code checks if the file exists and is read-only using the File.Exists() and File.GetAttributes() methods. If the file exists and is read-only, it is considered read-only.

  3. If the file is read-only, the code prints a message indicating that the file is read-only. Otherwise, the code prints a message indicating that the file is not read-only.

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

You can read Presentation.FullName property to get the file name, and check if the file is read-only, comes from a shared drive, or whether mark-of-the-web is set:

    static bool CheckMarkOfTheWeb(string filePath)
    {
        try
        {
            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var alternateDataStream = fileStream
                    .GetAlternateDataStream("Zone.Identifier", FileMode.Open, FileAccess.Read);

                return alternateDataStream != null;
            }
        }
        catch (IOException)
        {
            // Handle file access or stream-related exceptions.
            return false;
        }
        catch (Exception)
        {
            // Handle other exceptions.
            return false;
        }
    }

To check if a file comes from a network drive, use something like the following:

    static bool IsFileFromNetworkDrive(string filePath)
    {
        try
        {
            string drivePath = Path.GetPathRoot(filePath);
            DriveInfo driveInfo = new DriveInfo(drivePath);
            return driveInfo.DriveType == DriveType.Network;
        }
        catch (Exception)
        {
            // Handle exceptions, such as invalid file path or drive access issues.
            return false;
        }
    }

Upvotes: 0

Related Questions