PDA

View Full Version : سوال: فايل متني در #C



8502803
پنج شنبه 16 تیر 1390, 18:39 عصر
http://barnamenevis.org/images/icons/icon5.png يك فايل متني در #Cخواهشا كمك كنيد


با سلام من با #Cيك فايل متني ايجاد كردم ولي موقعي كه ميخوام ازش Print بگيرم در حالت preview داخل فايل خاليه؟ سوال بعدي اينكه وقتي يك فايل متني رو به صورت دستي پر ميكنم موقعي كه محتوياتش رو تو مثلا يك textboxنشون ميده خاليه ؟ لطفا كمك كنيد.:متفکر::متفکر::متفکر:
;()DialogResult result = openFileDialog1.ShowDialog
(if (result == DialogResult.OK
;filename = openFileDialog1.FileName



(""=!if (filename
}
;printDocument1.DocumentName = filename
;printPreviewDialog1.Document = printDocument1
;()printPreviewDialog1.ShowDialog
{

sinashahab
پنج شنبه 16 تیر 1390, 23:38 عصر
http://barnamenevis.org/images/icons/icon5.png يك فايل متني در #Cخواهشا كمك كنيد


سوال بعدي اينكه وقتي يك فايل متني رو به صورت دستي پر ميكنم موقعي كه محتوياتش رو تو مثلا يك textboxنشون ميده خاليه ؟ لطفا كمك كنيد.:متفکر::متفکر::متفکر:
{

شاید مشکل در read کردنتون هست.
من پیشنهاد میکنم از کلاس streamreader و StreamWriter استفاده کنید.

flash118
جمعه 17 تیر 1390, 12:03 عصر
با نام خدا
دوست عزیز امیدوارم که بتونم مشکلتون رو حل کنم خوب بسم الله:
برای نوشتن در فایل متی از کد زیر استفاده می کنیم:

using System;
using System.IO;

namespace write
{
class TextFileWriter
{
static void Main(string[] args)
{
// ایجاد یک شی برای نوشتن و همچنین باز کردن فایل مورد نظر
TextWriter tw = new StreamWriter("date.txt");

// نوشتن در یک خط از فایل متنی
tw.WriteLine(DateTime.Now);

// در پایین بستن شی باز شده
tw.Close();
}
}
}

خوب نمونه خروجی شما باید در فایل متنی به صورت:2/15/2011 8:54:51 PM
خوب اما برای خواندن:

using System;
using System.IO;

namespace reade
{
class TextFileReader
{
static void Main(string[] args)
{
// ایجاد ریدر و باز کردن فایل
Textreader tr = new StreamReader("date.txt");

// خواندن یک خط از فایل متنی
Console.WriteLine(tr.ReadLine());

// و بستن
tr.Close();
}
}
}

و اما این کد آخریه برای پرینت یک فایل متنی چند صحفه ای هست که اینو از MSDN برداشت کردم و تست شده هست:

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace PrintApp
{
public class Form1 : Form
{
private Button printButton;
private PrintDocument printDocument1 = new PrintDocument();
private string stringToPrint;
public Form1()
{
this.printButton = new System.Windows.Forms.Button();
this.printButton.Location = new System.Drawing.Point(12, 51);
this.printButton.Size = new System.Drawing.Size(75, 23);
this.printButton.Text = "Print";
this.printButton.Click += new System.EventHandler(this.printButton_Click);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.printButton);

// Associate the PrintPage event handler with the PrintPage event.
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
}

private void ReadFile()
{
string docName = "testPage.txt";
string docPath = @"c:\";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
stringToPrint = reader.ReadToEnd();
}
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;

// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);

// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);

// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);

// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}

private void printButton_Click(object sender, EventArgs e)
{
ReadFile();
printDocument1.Print();
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}

خوب امیدوارم مشکلتون حل شده باشه سعی کردم تا حد توان ساده بیان کنم اگه عیبی بود ببخشید موفق باشید