C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to get file path using open file dialog control


Posted Date: 15 May 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Abhishek AryaMember Level: Diamond    
Rating: Points: 10



The following code sample shows how to get file path using open file dialog control


OpenFileDialog ofd = new OpenFileDialog();

string filename = "";
string path = "";

if (ofd.ShowDialog() == DialogResult.OK)
{
filename = System.IO.Path.GetFileName(ofd.FileName);
path = System.IO.Path.GetDirectoryName(ofd.FileName);
}

MessageBox.Show(filename, "Filename");
MessageBox.Show(path, "Directory");




Responses

Author: RamyaNaidu    06 Aug 2008Member Level: Silver   Points : 2
private void btn_Click(object sender, EventArgs e)
{
openFileDialog.InitialDirectory = @":D\\";
openFileDialog.Filter = "Text files (*.txt)|*.txt|" +
"DWGfiles (*.*)|*.*";
openFileDialog.FilterIndex = 1;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(openFileDialog.FileName.ToString());
FileInfo File = new FileInfo("D:\\hhh.txt");
if (File.Exists)
{
StreamReader reader = new StreamReader(openFileDialog.FileName);
try
{
strFileName = openFileDialog.FileName;
txt.Text = reader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
finally
{
reader.Close();
MessageBox.Show("File exists");

}
}
else
{
FileInfo MyFile = new FileInfo(@"D:\\hhh.txt");
MyFile.Create();
MessageBox.Show("File Successfully Created.");
}

}
}


Author: RamyaNaidu    06 Aug 2008Member Level: Silver   Points : 2
private void btn_Click(object sender, EventArgs e)
{
openFileDialog.InitialDirectory = @":D\\";
openFileDialog.Filter = "Text files (*.txt)|*.txt|" +
"DWGfiles (*.*)|*.*";
openFileDialog.FilterIndex = 1;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(openFileDialog.FileName.ToString());
FileInfo File = new FileInfo("D:\\hhh.txt");
if (File.Exists)
{
StreamReader reader = new StreamReader(openFileDialog.FileName);
try
{
strFileName = openFileDialog.FileName;
txt.Text = reader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
finally
{
reader.Close();
MessageBox.Show("File exists");

}
}
else
{
FileInfo MyFile = new FileInfo(@"D:\\hhh.txt");
MyFile.Create();
MessageBox.Show("File Successfully Created.");
}

}
}


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Connect using TCP client
Previous Resource: How to open a file using System reader
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use