views:

124

answers:

2

when i run this code :

        Process printjob = new Process();
        printjob.StartInfo.FileName = "file.html";
        printjob.StartInfo.UseShellExecute = true;
        printjob.StartInfo.Verb = "print";
        printjob.StartInfo.CreateNoWindow = true;
        printjob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        printjob.Start();

this exception is thrown:
"No application is associated with the specified file for this operation"
what should i do?

+1  A: 

On your computer, no application is associated with the file-type ".html". If you're trying to view it in a web browser, consider starting iexplore.exe (for example, to launch internet explorer), and then include the file.html as a parameter.

For example:

Process.Start("IExplore.exe", @"C:\myPath\file.html");
Pandincus
I want to print the html file
saeed
How can I do that? with which application?
saeed
+1  A: 

The following code snippet should work, but it does have an issue that might be a deal breaker (continue reading for an explanation):

static void Main(string[] args)
{
    string pathToFile = "...";
    var processStartInfo = new ProcessStartInfo(); 
    processStartInfo.Verb = "print";
    processStartInfo.FileName = pathToFile;     

    var process = Process.Start(processStartInfo);
    process.WaitForExit();
}

The only problem with the code above is that it will show the print dialog. I was unable to find a way to supress it, and it seems to be an issue (or feature) specific to printing HTML files.

There's an ugly workaround if you can tolerate having the print dialog appearing for a second or so, and that is to simulate sending the "enter" key to the print dialog through code. The easiest way to do that is to use the System.Windows.Forms.SendKeys class, specifically the SendWait method.

So the revised code snippet will look like the following:

static void Main(string[] args)
{
    string pathToFile = "...";
    var processStartInfo = new ProcessStartInfo(); 
    processStartInfo.Verb = "print";
    processStartInfo.FileName = pathToFile;     

    var process = Process.Start(processStartInfo);

    System.Threading.Thread.Sleep(1000);
    System.Windows.Forms.SendKeys.SendWait("{ENTER}");

    process.WaitForExit();
}

The call to the Sleep is necessary to ensure that the print dialog is fully loaded and ready to receive user input before sending the key press.

HTH

ShaderOp
it has the problem yet.it seems that i have to set a program for printing an html file
saeed
Can you elaborate on the problem you're having? For example, What's the error messsage you're seeing? And if you right-click on an HTML file in Windows Explorer, do you see a "print" option in the context menu? If you don't see such an option, then you might want to try to fix that first.
ShaderOp
I don't see a print option there.how can I fix this new problem?!
saeed
I really don't have any idea how you would fix that. Taking a stab in the dark, you can try setting IE to be your default browser if it isn't the default already, or trying to install a newer version of IE if you don't have that already, or just wipe your machine and reinsall Windows if all else fails. But I think this is beyond the scope of your original question. Maybe you can try asking at superuser.com, which is probably a better venue for this new problem.
ShaderOp