1-) C# RMOS - Klasör veya Dosya Seçmek İçin Güzel Class
1-) C# RMOS - dosya.cs
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace RM_OCR {
public class dosya {
private OpenFileDialog dialog = new OpenFileDialog();
public OpenFileDialog Dialog {
get { return dialog; }
set { dialog = value; }
}
public new DialogResult ShowDialog() {
return this.ShowDialog(null);
}
public new DialogResult ShowDialog(IWin32Window owner) {
// Set validate names to false otherwise windows will not let you select "Folder Selection."
dialog.ValidateNames = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = true;
try {
// Set initial directory (used when dialog.FileName is set from outside)
if (dialog.FileName != null && dialog.FileName != "") {
if (Directory.Exists(dialog.FileName))
dialog.InitialDirectory = dialog.FileName;
else
dialog.InitialDirectory = Path.GetDirectoryName(dialog.FileName);
}
} catch (Exception ex) {
// Do nothing
}
// Always default to Folder Selection.
dialog.FileName = "Klasör Seç";
if (owner == null)
return dialog.ShowDialog();
else
return dialog.ShowDialog(owner);
}
public new DialogResult ShowDialogFile(IWin32Window owner) // sadece dosya seçmek için bunu kullan
{
// Set validate names to false otherwise windows will not let you select "Folder Selection."
dialog.ValidateNames = false;
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
try
{
// Set initial directory (used when dialog.FileName is set from outside)
if (dialog.FileName != null && dialog.FileName != "")
{
if (Directory.Exists(dialog.FileName))
dialog.InitialDirectory = dialog.FileName;
else
dialog.InitialDirectory = Path.GetDirectoryName(dialog.FileName);
}
}
catch (Exception ex)
{
// Do nothing
}
// Always default to Folder Selection.
dialog.FileName = "Klasör Seç";
if (owner == null)
return dialog.ShowDialog();
else
return dialog.ShowDialog(owner);
}
/// <summary>
// Helper property. Parses FilePath into either folder path (if Folder Selection. is set)
// or returns file path
/// </summary>
public string SelectedPath {
get {
try {
if (dialog.FileName != null &&
(dialog.FileName.EndsWith("Folder Selection.") || !File.Exists(dialog.FileName)) &&
!Directory.Exists(dialog.FileName)) {
return Path.GetDirectoryName(dialog.FileName);
} else {
return dialog.FileName;
}
} catch (Exception ex) {
return dialog.FileName;
}
}
set {
if (value != null && value != "") {
dialog.FileName = value;
}
}
}
/// <summary>
/// When multiple files are selected returns them as semi-colon seprated string
/// </summary>
public string SelectedPaths {
get {
if (dialog.FileNames != null && dialog.FileNames.Length > 1) {
StringBuilder sb = new StringBuilder();
foreach (string fileName in dialog.FileNames) {
try {
if (File.Exists(fileName))
sb.Append(fileName + ";");
} catch (Exception ex) {
// Go to next
}
}
return sb.ToString();
} else {
return null;
}
}
}
}
}
2-) MyGetTip metodu
public string MyGetTip(ref string path)
{
try
{
dosya asd = new dosya();
asd.ShowDialog();
if (asd.Dialog.FileName.Contains("Klasör Seç"))
{ // eğer dizin seçmiş ise
path = asd.SelectedPath;
return "dizin";
}
else
{
path = asd.SelectedPath;
//path=Directory.GetParent(asd.SelectedPath).ToString(); // UYARI BURAYI AÇARSAN SADECE KLASÖR SEÇER
return "dosya";
}
}
catch (Exception ex)
{
MessageBox.Show("HATA ! " + ex.Message);
return "";
}
}
3-) Kullanımı
string path = "";
string W_tip = MyGetTip(ref path);
if (W_tip.Equals("dizin"))
{
Console.WriteLine("dizin seçtiniz = "+path);
}
else if (W_tip.Equals("dosya"))
{
Console.WriteLine("dosya seçtiniz "+path);
}
3-) Kullanımı sadece dosya için
string path = "";
string W_tip = MyGetTipDosya(ref path);
if (W_tip.Equals("dosya"))
{
Console.WriteLine("dosya seçtiniz " + path);
textBox1.Text = path;
}
public string MyGetTipDosya(ref string path)
{
try
{
dosya asd = new dosya();
// asd.ShowDialog(null);
asd.ShowDialogFile(null);
if (asd.Dialog.FileName.Contains("Dosya Seç"))
{ // eğer dizin seçmiş ise
path = asd.SelectedPath;
return "dizin";
}
else
{
path = asd.SelectedPath;
return "dosya";
}
}
catch (Exception ex)
{
MessageBox.Show("HATA ! " + ex.Message);
return "";
}
}
3-) Kullanımı sadece klasör için
private void btnNereye_Click(object sender, EventArgs e)
{
string path = "";
string W_tip = MyGetTip(ref path);
if (W_tip.Equals("dizin"))
{
Console.WriteLine("dizin seçtiniz = " + path);
textBox2.Text = path;
}
else if (W_tip.Equals("dosya"))
{
Console.WriteLine("dosya seçtiniz " + path);
textBox2.Text = path;
}
}
public string MyGetTip(ref string path)
{
try
{
dosya asd = new dosya();
asd.ShowDialog();
if (asd.Dialog.FileName.Contains("Klasör Seç"))
{ // eğer dizin seçmiş ise
path = asd.SelectedPath;
return "dizin";
}
else
{
path = asd.SelectedPath;
path=Directory.GetParent(asd.SelectedPath).ToString(); // UYARI BURAYI AÇARSAN SADECE KLASÖR SEÇER
return "dosya";
}
}
catch (Exception ex)
{
MessageBox.Show("HATA ! " + ex.Message);
return "";
}
}