Cześć wszystkim,
mam pytanko, dosyć proste dla większości z Was zapewne,
tyczy się one sytuacji, kiedy używamy klasy StreamWriter, czy trzebą ją zamykać jakoś w stylu takim jak jest to umieszczone w komentarzach w poniższym voidz'ie?
public void SaveData()
{
int i = 0;
string path = @"..\..\..\Database\accounts.txt";
string ndPath = @"..\..\..\Database\" + address + ".txt";
string[] lines =
{
address,
username,
password,
addInfo
};
if (!File.Exists(path)) //if not exists -> create accounts.txt
{
using (StreamWriter sw = File.CreateText(path)) ;
}
using (StreamWriter outputFile = new StreamWriter(path, true)) //write name of address to faster searching
{
outputFile.WriteLine(address);
using (StreamWriter sw = File.CreateText(ndPath)) ; //create new file - his name is address of site
foreach (string line in lines)
{
using (StreamWriter outputFile2 = new StreamWriter(ndPath, true))
{
outputFile2.WriteLine(line);
//outputFile2.Close() A JESLI TU ZAMYKAM, TO I TAK NORMALNIE WSZYSTKO SIE ZAPISZE DO PLIKU, DOPIERO JAK DAM RETURNA W TYM MIEJSCU TO TYLKO PIEWRSZA LINIJKA SIE ZAPISZE...
}
//outputFile2.Close(); NIE DA SIE ZAMKNAC Z TEGO POZIOMU
}
//outputFile.Close(); POTRZEBNY?
}
}
Pytam, ponieważ w C++, w którym miałem szczęście kiedyś spędzać czas, biblioteke odnośnie wchodzenia do pliku trzeba było zamykać*. Wie ktoś może czy taka sytuacja powinna mieć miejsce?