Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1841 | Besucher online: 5
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Prüfen ob eine Datei druckbar ist


Autor: Jörg Meyer
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 1857
  
Kick it on dotnet-kicks.de  

Beschreibung:

Wie rendert man schnell Dokumente? Indem man Sie per "print" über System.diagnostics druckt... doch wie bekommt man raus, ob es eine Anwendung gibt die es erlaubt das diese Datei gedruckt wird? Ist etwas für die Shell als "print" Verb definiert?

Dieser "Schnippsel" prüft ob eine Datei druckbar ist. Wichtig hier ist, dass das ganze nur bis einschliesslich Windows Vista 100% arbeitet, bzw. auf 32/64 Bit Systemem bis dahin - ab Windows 7 bzw. Server 2008 R2 wird es, bedingt durch die Trennung von der Registry von 32 und 64 Bit ggf. zu falschen Informationen kommen, wenn das "Druckprogramm" ein 64 Bit Programm ist (also als solches registriert ist).
Zudem muss auf Server 2008 R2 zuvür die 32 Bit Unterstützung installiert sein.

Aus Sicherheitsgründen sollte man noch die Prüfung einbauen ob es eine EXE Datei ist, sonst freuen sich die Unholde.

Etwas reines .NET mässiges gibt es leider noch nicht...


Abgelegt unter: printable file, printable, druckbare Datei, ermitteln ob Datei druckbar ist.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace ctm.helpers
{
    /// <summary>
    /// Helperclass for Win32 shlwapi
    /// </summary>
    public class shlwapi
    {
        [DllImport("shlwapi.dll")]
        extern static int AssocCreate(ref Guid clsid,ref Guid riid,[MarshalAs(UnmanagedType.Interface)] out object ppv);

        [Flags]
        enum ASSOCF
        {
            INIT_NOREMAPCLSID = 0x00000001,
            INIT_BYEXENAME = 0x00000002,
            OPEN_BYEXENAME = 0x00000002,
            INIT_DEFAULTTOSTAR = 0x00000004,
            INIT_DEFAULTTOFOLDER = 0x00000008,
            NOUSERSETTINGS = 0x00000010,
            NOTRUNCATE = 0x00000020,
            VERIFY = 0x00000040,
            REMAPRUNDLL = 0x00000080,
            NOFIXUPS = 0x00000100,
            IGNOREBASECLASS = 0x00000200,
            INIT_IGNOREUNKNOWN = 0x00000400
        }

        enum ASSOCSTR
        {
            COMMAND = 1,
            EXECUTABLE,
            FRIENDLYDOCNAME,
            FRIENDLYAPPNAME,
            NOOPEN,
            SHELLNEWVALUE,
            DDECOMMAND,
            DDEIFEXEC,
            DDEAPPLICATION,
            DDETOPIC,
            INFOTIP,
            QUICKTIP,
            TILEINFO,
            CONTENTTYPE,
            DEFAULTICON,
            SHELLEXTENSION
        }

        enum ASSOCKEY
        {
            SHELLEXECCLASS = 1,
            APP,
            CLASS,
            BASECLASS
        }

        enum ASSOCDATA
        {
            MSIDESCRIPTOR = 1,
            NOACTIVATEHANDLER,
            QUERYCLASSSTORE,
            HASPERUSERASSOC,
            EDITFLAGS,
            VALUE
        }

        [Guid("c46ca590-3c3f-11d2-bee6-0000f805ca57"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface IQueryAssociations
        {
            void Init(
              [In] ASSOCF flags,
              [In, MarshalAs(UnmanagedType.LPWStr)] string pszAssoc,
              [In] UIntPtr hkProgid,
              [In] IntPtr hwnd);

            void GetString(
              [In] ASSOCF flags,
              [In] ASSOCSTR str,
              [In, MarshalAs(UnmanagedType.LPWStr)] string pwszExtra,
              [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszOut,
              [In, Out] ref int pcchOut);

            void GetKey(
              [In] ASSOCF flags,
              [In] ASSOCKEY str,
              [In, MarshalAs(UnmanagedType.LPWStr)] string pwszExtra,
              [Out] out UIntPtr phkeyOut);

            void GetData(
              [In] ASSOCF flags,
              [In] ASSOCDATA data,
              [In, MarshalAs(UnmanagedType.LPWStr)] string pwszExtra,
              [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)] out byte[] pvOut,
              [In, Out] ref int pcbOut);

            void GetEnum(); // not used actually
        }

        static Guid CLSID_QueryAssociations = new Guid("a07034fd-6caa-4954-ac3f-97a27216f98a");
        static Guid IID_IQueryAssociations = new Guid("c46ca590-3c3f-11d2-bee6-0000f805ca57");

        /// <summary>
        /// Check if named File is a printable File
        /// </summary>
        /// <param name="aFileName">Full Name of File which must be checked.</param>
        /// <returns>Boolean value : true if printable, false if no Application is registered to print</returns>
        public Boolean IsPrintableFile(String aFileName)
        {
            if (aFileName == String.Empty)
            {
                return false;
            }
            try
            {
                object obj;
                AssocCreate(ref CLSID_QueryAssociations,ref IID_IQueryAssociations,out obj);
                IQueryAssociations qa = (IQueryAssociations)obj;
                qa.Init(ASSOCF.INIT_DEFAULTTOSTAR,Convert.ToString(Path.GetExtension(aFileName)),UIntPtr.Zero, IntPtr.Zero);

                int size = 0;
                qa.GetString(ASSOCF.NOTRUNCATE, ASSOCSTR.COMMAND,
                  "print", null, ref size);

                StringBuilder sb = new StringBuilder(size);
                qa.GetString(ASSOCF.NOTRUNCATE, ASSOCSTR.COMMAND,
                  "print", sb, ref size);

                return true;
            }
            catch (Exception e)
            {
                if ((uint)Marshal.GetHRForException(e) == 0x80070483)
                    return false;
                else
                    return false;
            }
        }
    }
}

Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:
Keine Snippets verfügbar.

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.