Printer properties silverlight com-interop and out of browser

心已入冬 提交于 2019-12-08 01:32:26

问题


I need to access a printer to know if is active and have paper.

I'm using silverlight 4 out of box with elevated trust.

At this point I have:

 string cFileName = "temp.txt";
 string cAction = "print";
 using (dynamic fso = AutomationFactory.CreateObject(@"Scripting.FileSystemObject"))
        {
            dynamic file = fso.CreateTextFile(cFileName, true);
            file.WriteLine("First line text");
            file.WriteLine("second line text");
            file.Close();
        }
  //Print
 dynamic shell = AutomationFactory.CreateObject("Shell.Application");
 shell.ShellExecute(cFileName, "", "", cAction, 1);

I'm using a temp file txt to creat my print page.

By the way, its possible to send a string or something like that directly to printer?

With that code I can not see if printer is active or see if the operations was successfully done.

I saw that I can use in C# using System.Drawing.Printing; or using System.Management; but I think its not possible with silverlight.

Some advice?

--EDIT--

I find a solution. Right now I'm using the Win32_Printer Class to get info from printer.

string name;
int printerStatus;
int detectedErrorState;

using (dynamic locatorService = AutomationFactory.CreateObject("WbemScripting.SWbemLocator"))
{
    locatorService.Security_.ImpersonationLevel = 3;
    locatorService.Security_.AuthenticationLevel = 4;
    var wmiService = locatorService.ConnectServer(".", @"root\cimv2");

    using (dynamic result = wmiService.ExecQuery("Select * from Win32_Printer where Default = True"))
    {
        name = result.ItemIndex(0).Name;
        printerStatus = result.ItemIndex(0).PrinterStatus;
        detectedErrorState = result.ItemIndex(0).DetectedErrorState;
    }
}

link: http://msdn.microsoft.com/en-us/library/aa394363(v=VS.85).aspx

Now I'm trying to print without a temp file.txt.

I wanna print directly from code to printer.

If someone have advice let me know.

来源:https://stackoverflow.com/questions/6954276/printer-properties-silverlight-com-interop-and-out-of-browser

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