PDA

View Full Version : سوال: اشکال در ذخیره سازی فایل پس از ویرایش



mujtaba20
پنج شنبه 26 مرداد 1391, 17:30 عصر
سلام خدمت اساتید عزیز
یک برنامه نوشتم که با فایل های مختلف کار کنه، اما زمانی که فایل رو پس از باز کردن درون یک آرایه از جنس بایت می ریزم و پس از ویرایش ذخیره می کنم فایل رو به صورت خراب ذخیره می کنه و دیگه نمیشه ازش استفاده کرد. لطفا به من راهنمایی بدید.
با تشکر مجتبی :اشتباه:

amir4015
پنج شنبه 26 مرداد 1391, 17:33 عصر
منظورت فايل txt هست؟

ali_habibi1384
پنج شنبه 26 مرداد 1391, 17:34 عصر
نحوه نوشتن اطلاعات در فايلها (http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx)

tooraj_azizi_1035
پنج شنبه 26 مرداد 1391, 17:48 عصر
یعنی با این متد کار کردی و مشکل پیدا کردی؟
File.WriteAllBytes(string path, byte[] bytes)

با BinaryWriter و BinaryReader کار کن:

using System;
using System.IO;
using System.Security.Permissions;

// Store and retrieve application settings.
class AppSettings
{
const string fileName = "AppSettings#@@#.dat";
float aspectRatio;
string lookupDir;
int autoSaveTime;
bool showStatusBar;

public AppSettings()
{
// Create default application settings.
aspectRatio = 1.3333F;
lookupDir = @"C:\AppDirectory";
autoSaveTime = 30;
showStatusBar = false;

if(File.Exists(fileName))
{
BinaryReader binReader =
new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
// If the file is not empty,
// read the application settings.
// First read 4 bytes into a buffer to
// determine if the file is empty.
byte[] testArray = new byte[3];
int count = binReader.Read(testArray, 0, 3);

if (count != 0)
{
// Reset the position in the stream to zero.
binReader.BaseStream.Seek(0, SeekOrigin.Begin);

aspectRatio = binReader.ReadSingle();
lookupDir = binReader.ReadString();
autoSaveTime = binReader.ReadInt32();
showStatusBar = binReader.ReadBoolean();
}
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException e)
{
Console.WriteLine("{0} caught and ignored. " +
"Using default values.", e.GetType().Name);
}
finally
{
binReader.Close();
}
}

}

// Create a file and store the application settings.
public void Close()
{
using(BinaryWriter binWriter =
new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
binWriter.Write(aspectRatio);
binWriter.Write(lookupDir);
binWriter.Write(autoSaveTime);
binWriter.Write(showStatusBar);
}
}

public float AspectRatio
{
get{ return aspectRatio; }
set{ aspectRatio = value; }
}

public string LookupDir
{
get{ return lookupDir; }
set{ lookupDir = value; }
}

public int AutoSaveTime
{
get{ return autoSaveTime; }
set{ autoSaveTime = value; }
}

public bool ShowStatusBar
{
get{ return showStatusBar; }
set{ showStatusBar = value; }
}
}

class Test
{
static void Main()
{
// Load application settings.
AppSettings appSettings = new AppSettings();
Console.WriteLine("App settings:\nAspect Ratio: {0}, " +
"Lookup directory: {1},\nAuto save time: {2} minutes, " +
"Show status bar: {3}\n",
new Object[4]{appSettings.AspectRatio.ToString(),
appSettings.LookupDir, appSettings.AutoSaveTime.ToString(),
appSettings.ShowStatusBar.ToString()});

// Change the settings.
appSettings.AspectRatio = 1.250F;
appSettings.LookupDir = @"C:\Temp";
appSettings.AutoSaveTime = 10;
appSettings.ShowStatusBar = true;

// Save the new settings.
appSettings.Close();
}
}