Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing By Using Port

Status
Not open for further replies.

whloo

Programmer
Apr 14, 2003
168
SG
Hi,

I am trying to write a function that allow all of my user to print using my allocated printer for them. It is because some of the users are not allowed to print for some reason hence their local pc don't have any installed default printer at all.
i was thinking is it possible if i put in my code to allow them to direct print to my designated printer?
If so, how?

Your help will be greatly appreciated!
Thanks!

Regards,
weihann.
 
1.
Create a process on the remote machine name that has installed the printer
Find a way that this machine can access the file you want to print and just print it.


public class MyProcess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;

/// <summary>
/// Prints a file with a .doc extension.
/// </summary>
public void PrintDoc()
{
Process myProcess = new Process();
myProcess.MachineName = &quot;myremotemachine&quot;; // without \
try
{
// Get the path that stores user documents.
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);

myProcess.StartInfo.FileName = myDocumentsPath + &quot;\\MyFile.doc&quot;;
myProcess.StartInfo.Verb = &quot;Print&quot;;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + &quot;. Check the path.&quot;);
}

else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message + &quot;. You do not have permission to print this file.&quot;);
}
}
}


public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.PrintDoc();
}
}


2. Implement your application as client and another application as a service that is running on the machine where the printer is working.
Use System.Net.Sockets class to implement client/server.
On the server side the above code still to be used but you can improve it by adding the notifications to the client by adding the followings:


myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(captureOut);
private void captureOut(object sender, EventArgs e)
{
// notify the user (client application) here
}
More than that, you could have above code in a thread that is doing only the Print and is waiting for the server application when it is done.
I hope this help.
-obislavu-
 
obislavu,

thanks for your reply.
so sorry for mislead you.
My application is a web application. Is it possible for me to do that?
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top