Click here to Skip to main content
15,889,528 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i run my program it gives me error:
No application is associated with the specified file for this operation
code is:
C#
Process p = null;
           try
           {
               p = new Process();
               p.StartInfo.FileName = "C:\\Test.txt";
               p.StartInfo.Verb = "Print";
               p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
               p.StartInfo.UseShellExecute = true;
               p.Start();
               p.WaitForExit();               
           }
           catch
           {
               MessageBox.Show(e.ToString());
           }

plz some one help me
Posted
Comments
Sergey Alexandrovich Kryukov 9-Apr-13 2:37am    
Why doing all that dirty stuff? What's the problem, how to print with C#? By NOT trying to use notepad...
—SA

1 solution

Please, read Sergey's comment and don't do it using Notepad.

Follow these links to find solution with examples:
Code: Printing a Text File (Visual C#)[^]
Printing Text File in C#[^]
 
Share this answer
 
Comments
Member 15028314 8-Aug-22 19:31pm    
I've put together a "C" routine that builds a nice sized calendar with some features while writing it to a text file.
When completed, I edited the file just to look at it via MS-Notepad and it looked like garbage. Then I printed
it via MS-Notepad and it resulted in printed garbage. BUT when using NOTEPAD++ editor to look at them, the
calendars retained their shapes and forms, and then I printed them out using NOTEPAD++, their results were
spectacular. Included below is some C# printer code that I'm using to print out these text files, but their results
were garbage, they don't hold their form. How can this C# printer code be doctored/modified up to
perform/PRINT like NOTEPAD++, which produces some excellent results. The results being some excellent
calendar forms.

Any help would be appreciated. Again Thanks.
Aug 8, 2022
Member 15028314 - McGruff.
-----------------------
private void printbutton1_Click(object sender, EventArgs e)
{
int ierrcode;
errorline4.Text = "";
ierrcode = 0;
dofileactivitys( out ierrcode); //// pulling some edits.
if (ierrcode != 0)
{ //// the error section.
/// display an error message here.......
errorline4.Text = " Error Encountered in printing operations. ";
}
else
{
string filename;
filename = gstring;
Console.WriteLine(" *File: " + filename);
///Create a StreamReader object
reader = new StreamReader(filename);
///Create a Verdana font with size 10
verdana10Font = new Font("Verdana", 10);

///Create a PrintDocument object
PrintDocument pd = new PrintDocument();
///Add PrintPage event handler
pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
///Call Print Method
pd.Print();
///Close the reader
if (reader != null)
{
reader.Close();
}
}
}
private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)
{
//Get the Graphics object
Graphics g = ppeArgs.Graphics;
float linesPerPage = 0;
float yPos = 0;
int count = 0;
//Read margins from PrintPageEventArgs
float leftMargin = ppeArgs.MarginBounds.Left;
float topMargin = ppeArgs.MarginBounds.Top;
string line = null;
//Calculate the lines per page on the basis of the height of the page and the height of the font
linesPerPage = ppeArgs.MarginBounds.Height / verdana10Font.GetHeight(g);
//Now read lines one by one, using StreamReader
while (count < linesPerPage && ((line = reader.ReadLine()) != null))
{
//Calculate the starting position
yPos = topMargin + (count * verdana10Font.GetHeight(g));
//Draw text
g.DrawString(line, verdana10Font, Brushes.Black, leftMargin, yPos, new StringFormat());
//Move to next line
count++;
}
//If PrintPageEventArgs has more pages to print
if (line != null)
{
ppeArgs.HasMorePages = true;
}
else
{
ppeArgs.HasMorePages = false;
}
}
------------------------------

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900