PDA

View Full Version : کار با فایل ها در ‍#C



ermia2008
سه شنبه 23 مرداد 1386, 16:31 عصر
سلام به همگی. لطفا اگه کسی کار با فایل ها در ‍#C رو بلده یه مثال برای طرز خوندن ونوشتن تو فایل برام بفرسته.ممنون.

PC2st
سه شنبه 23 مرداد 1386, 16:43 عصر
برای خواندن از یک فایل متنی از متد System.IO.File.ReadAllLines یا System.IO.File.ReadAllText و برای نوشتن در یک فایل متنی، از متد System.IO.File.WriteAllLines یا System.IO.File.WriteAllText استفاده کنید.

rasoul_ras
سه شنبه 23 مرداد 1386, 16:57 عصر
http://barnamenevis.org/forum/attachment.php?attachmentid=10291&d=1187094384
البته کار با فایلهای bmp

MH2538
چهارشنبه 24 مرداد 1386, 09:52 صبح
سلام
مثال خود MSDN


using System;
using System.IO;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}

// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}

try
{
string path2 = path + "temp";
// Ensure that the target does not exist.
File.Delete(path2);

// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);

// Delete the newly created file.
File.Delete(path2);
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}