How Can I Check That A File Is A Valid XPS File With C#?

痞子三分冷 提交于 2019-11-29 16:40:21

The following will distinguish XPS files from other ZIP archives and non-ZIP files. It won't determine whether the file is fully-valid XPS - for that you would need to load each page.

using System;
using System.IO;
using System.Windows.Xps.Packaging;

class Tester
{
    public static bool IsXps(string filename)
    {
        try
        {
            XpsDocument x = new XpsDocument(filename, FileAccess.Read);

            IXpsFixedDocumentSequenceReader fdsr = x.FixedDocumentSequenceReader;

            // Needed to actually try to find the FixedDocumentSequence
            Uri uri = fdsr.Uri;

            return true;
        }
        catch (Exception)
        {
        }

        return false;
    }
}

You can check for the content Type of the file instead of the file extension.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!