开发者

Visual Studio file selector

开发者 https://www.devze.com 2022-12-10 23:21 出处:网络
I am new to .NET and the Visual Studio IDE.How to I add to a form a component that lets the user browse the file system开发者_开发百科 to select a certain file which will then allow me to programmatic

I am new to .NET and the Visual Studio IDE. How to I add to a form a component that lets the user browse the file system开发者_开发百科 to select a certain file which will then allow me to programmatically use the selected path as a string variable?


this should do the trick:

string path;
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
    path = file.FileName;
}

the string path should now contain the selected file path

**Edit: ** As mentioned in a comment below, OpenFileDialog is disposable so should be wrapped in a using statement.


OpenFileDialog should suit your needs. You'll probably need to put a button (or some other clickable type UI element) on the page that will pop the dialog up. Then once the user has selected a file and clicked "OK" you'll just check the response for which file was selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号