Merhaba,
C# ile projelerimizde sıklıkla kullandığımız dosya ve klasör işlemlerini aşağıdaki örneklerde bulabilirsiniz.
C# Klasör İşlemleri
C# Yeni Klasör Oluşturma
1 2 3 4 5 6 7 8 | Directory<span class="token punctuation">.</span><span class="token function">CreateDirectory</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"YeniKlasör"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//Proje klasörü içerisindeki bin/debug in içerisine YeniKlasör isimli bir klasör açar.</span></span> <span class="token comment" spellcheck="true"><span class="hljs-comment">// Eğer exe'yi başka klasöre çıkarttıysanız o klasöre açar</span></span> Directory<span class="token punctuation">.</span><span class="token function">CreateDirectory</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">@"C:YeniKlasör"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//C'ye YeniKlasör isminde bir klasör açar.</span></span> |
C# Klasör Silme
1 2 3 4 5 6 7 | Klasör Silme Directory<span class="token punctuation">.</span><span class="token function"><span class="hljs-operator"><span class="hljs-keyword">Delete</span></span></span><span class="token punctuation"><span class="hljs-operator">(</span></span><span class="token string"><span class="hljs-operator">@<span class="hljs-string">"C:YeniKlasör"</span></span></span><span class="token punctuation"><span class="hljs-operator">)</span></span><span class="token punctuation"><span class="hljs-operator">;</span></span> <span class="token comment" spellcheck="true">//C' deki içi boş olan YeniKlasör’ü siler.</span> Directory<span class="token punctuation">.</span><span class="token function"><span class="hljs-operator"><span class="hljs-keyword">Delete</span></span></span><span class="token punctuation"><span class="hljs-operator">(</span></span><span class="token string"><span class="hljs-operator">@<span class="hljs-string">"C:YeniKlasör"</span></span></span><span class="token punctuation"><span class="hljs-operator">,</span></span><span class="token keyword"><span class="hljs-operator"><span class="hljs-keyword">true</span></span></span><span class="token punctuation"><span class="hljs-operator">)</span></span><span class="token punctuation"><span class="hljs-operator">;</span></span> <span class="token comment" spellcheck="true">//C' deki YeniKlasör’ü dolu mu boş mu diye kontrol etmeden siler. </span> |
C# Klasör Bilgilerini Alma
1 2 3 4 5 6 7 8 9 10 11 12 13 | DateTime OluşturmaZamani <span class="token operator">=</span> Directory<span class="token punctuation">.</span><span class="token function">GetCreationTime</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"YeniKlasör"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> MessageBox<span class="token punctuation">.</span><span class="token function">Show</span><span class="token punctuation">(</span>OluşturmaZamani <span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//YeniKlasörün oluşturulduğu tarih ve zamanı gösterir.</span></span> DateTime SonErisimZamani <span class="token operator">=</span> Directory<span class="token punctuation">.</span><span class="token function">GetLastAccessTime</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"YeniKlasör"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> MessageBox<span class="token punctuation">.</span><span class="token function">Show</span><span class="token punctuation">(</span>SonErisimZamani <span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//YeniKlasöre en son sağlanan erişim zamanın bilgisini döndürür.</span></span> DateTime SonKayitZamani <span class="token operator">=</span> Directory<span class="token punctuation">.</span><span class="token function">GetLastWriteTime</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"YeniKlasör"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> MessageBox<span class="token punctuation">.</span><span class="token function">Show</span><span class="token punctuation">(</span>SonKayitZamani <span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//YeniKlasöre en son veri yazıldığı zamanın bilgisini döndürür.</span></span> |
C# Klasör Taşıma
1 2 3 4 5 6 | <span class="hljs-constant">Klas</span>ör <span class="hljs-constant">Ta</span>şıma <span class="hljs-constant">Directory</span><span class="token punctuation">.</span><span class="token function"><span class="hljs-constant">Move</span></span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"YeniKlasör"</span></span><span class="token punctuation">,</span><span class="token string">@<span class="hljs-string">"C:YeniKlasör"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-regexp">//</span><span class="hljs-constant">YeniKlas</span>örü ilk parametredeki yerden alıp, ikinci parametredeki yere taşır.</span> <span class="token comment" spellcheck="true">/<span class="hljs-regexp">/Verilen örneğe göre (bin/debug</span><span class="hljs-string">'dan C'</span>ye)</span> |
C# Klasör Varlığı Kontrolü
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token comment" spellcheck="true"><span class="hljs-comment">// Burada cevap true yada false döner.</span></span> <span class="token keyword"><span class="hljs-keyword">if</span></span> <span class="token punctuation">(</span>Directory<span class="token punctuation">.</span><span class="token function">Exists</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">@"c:Program Files"</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> MessageBox<span class="token punctuation">.</span><span class="token function">Show</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"Var"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//Klasör Varsa Çalışır</span></span> <span class="token punctuation">}</span> <span class="token keyword"><span class="hljs-keyword">else</span></span> <span class="token punctuation">{</span> MessageBox<span class="token punctuation">.</span><span class="token function">Show</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">"Yok"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//Klasör Yoksa Çalışır</span></span> <span class="token punctuation">}</span> |
C# Dosya İşlemleri
C# Yeni Dosya Oluşturma
1 2 3 4 5 6 7 | FileStream fs<span class="token operator">=</span> File<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">@"C:ExportReportsReport.txt"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//C'dekiExportReports klasörünün içine Report adında bir metin dosyası oluşturur.</span></span> fs<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//Create ile bir dosya oluşturulduğunda Filestream nesnesi döner.</span></span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//FileStream kapatılmadan dosya kopyalama ve taşıma işlemleri yapılamaz. Çünkü dosya hala açıktır.</span></span> |
C# Dosya Kopyalama
1 2 3 4 | File<span class="token punctuation">.</span><span class="token function">Copy</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">@"C:ExportReportsReport.txt"</span></span><span class="token punctuation">,</span> <span class="token string"><span class="hljs-string">@"C:Report.txt"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//İlk parametredeki dosya, ikinci parametredeki adrese kopyalanır.</span></span> |
C# Dosya Taşıma
1 2 3 4 | File<span class="token punctuation">.</span><span class="token function">Move</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string">@"C:ExportReportsReport.txt"</span></span><span class="token punctuation">,</span> <span class="token string"><span class="hljs-string">@"C:Report.txt"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"><span class="hljs-comment">//İlk parametredeki dosya, ikinci parametredeki adrese taşınır.</span></span> |
C# Dosya Silme
1 2 3 4 5 | File<span class="token punctuation">.</span><span class="token function"><span class="hljs-operator"><span class="hljs-keyword">Delete</span></span></span><span class="token punctuation"><span class="hljs-operator">(</span></span><span class="token string"><span class="hljs-operator">@<span class="hljs-string">"C:ExportReportsReport.txt"</span></span></span><span class="token punctuation"><span class="hljs-operator">)</span></span><span class="token punctuation"><span class="hljs-operator">;</span></span> <span class="token comment" spellcheck="true">// Dosyayı siler.</span> |
C# Dosya Varlığı Kontrolü
1 2 3 4 5 6 7 8 | File<span class="token punctuation">.</span><span class="token function"><span class="hljs-operator"><span class="hljs-keyword">Delete</span></span></span><span class="token punctuation"><span class="hljs-operator">(</span></span><span class="token string"><span class="hljs-operator">@<span class="hljs-string">"C:ExportReportsReport.txt"</span></span></span><span class="token punctuation"><span class="hljs-operator">)</span></span><span class="token punctuation"><span class="hljs-operator">;</span></span> <span class="token comment" spellcheck="true">// Dosyayı siler.</span> Dosya Varlığı Kontrolü File<span class="token punctuation">.</span><span class="token function"><span class="hljs-operator"><span class="hljs-keyword">Delete</span></span></span><span class="token punctuation"><span class="hljs-operator">(</span></span><span class="token string"><span class="hljs-operator">@<span class="hljs-string">"C:ExportReportsReport.txt"</span></span></span><span class="token punctuation"><span class="hljs-operator">)</span></span><span class="token punctuation"><span class="hljs-operator">;</span></span> <span class="token comment" spellcheck="true">// Dosyayı siler.</span> |