Ƙirƙirar cikakken shirin don sarrafa tsarin fayiloli a cikin takamaiman kundin adireshi na iya zama ƙwaya mai wahala don tsattsage, musamman lokacin da kuke nutsewa cikin coding a karon farko. Sa'ar al'amarin shine, C # yaren shirye-shirye yana sauƙaƙa wannan aikin tare da ingantattun ɗakunan karatu da kuma hanya mai sauƙi.
A yayin wannan labarin, za mu fara bayyana abin da ya sa C # ɗaya daga cikin mafi kyawun dandamali don irin wannan aikin. Sa'an nan, za mu nutse daidai cikin mafita, bayyana kowane mataki daki-daki. Na gaba, za mu mayar da hankali kan mahimmancin wasu ɗakunan karatu na C # da kuma yadda suke taka muhimmiyar rawa wajen magance wannan matsala.
Me yasa C # don sarrafa fayil?
C#, yaren shirye-shirye da yawa da Microsoft ya haɓaka, yana da abubuwa da yawa da za a iya bayarwa idan ya zo ga sarrafa fayil. Yana da ƙarfi .NET tsarin yana bawa masu haɓakawa damar ƙirƙira, karantawa, rubuta, da share fayiloli kawai amma kuma su zurfafa cikin kundayen adireshi don fitar da cikakkun bayanai. Sauƙin sarrafa tsarin fayil ya sa C # ya zama sanannen harshe don magance irin waɗannan ayyuka.
Yadda ake samun adadin fayiloli a cikin kundin adireshi tare da C #?
Yanzu, bari mu shiga cikin mafita. C # yana samar muku da abubuwan Tsarin.IO sarari suna daga inda za mu iya amfani da ajin Directory. Wannan ajin an sanye shi da wata hanya mai suna GetFiles, wacce ke taimakawa wajen samun kirga fayiloli.
Ga guntattakin:
amfani da Tsarin;
ta amfani da System.IO;
aji Shirin
{
a tsaye babu Main()
{
kirtani[] fayiloli = Directory.GetFiles("C:\Your_Directory");
Console.WriteLine ("Yawan fayiloli: {0}", fayiloli. Tsawon);
}
}
The GetFiles Hanyar samun fayiloli a cikin littafin da aka ba (wakilta a matsayin 'Your_Directory' a cikin lambar), sa'an nan kuma tsawon kadarorin yana ba da lissafin fayiloli.
Cikakken bayanin lambar
Yana da mahimmanci a fahimci abin da ke bayan fage lokacin da aka aiwatar da lambar.
- Na farko, an haɗa wuraren suna 'System' da 'System.IO' don amfani da ajin Directory da Console.
- Babbar hanya ta fara ajin Shirin don fara aiwatar da shirin.
- Hanyar GetFiles na ajin Directory tana tattara hanyar fayil daga ƙayyadadden jagorar.
- Ana adana hanyoyin sunan fayil a cikin tsararrun kirtani na 'fiyiloli'.
- A ƙarshe, ana buga tsayin jeri (watau adadin fayiloli) ta amfani da hanyar WriteLine Console.
Binciko masu alaƙa da ɗakunan karatu na C # ko Ayyuka
The Tsarin.IO suna sanye take da ayyuka da azuzuwa da yawa (kamar 'File', 'Path', 'StreamReader', 'StreamWriter', da sauransu) baya ga 'Directory' don biyan ƙarin buƙatun sarrafa fayil. Kowane ɗayan waɗannan azuzuwan suna da mahimmanci yayin ma'amala da bangarori daban-daban na sarrafa fayil a cikin C #.