Wydobywanie ścieżki z OpenFileDialog ścieżka/nazwa pliku
Piszę małe narzędzie, które zaczyna się od wybrania pliku, a następnie muszę wybrać folder. Chciałbym ustawić domyślny folder, w którym znajdował się wybrany plik.
OpenFileDialog.FileName
zwraca pełna ścieżka & nazwa pliku - chcę uzyskać tylko część ścieżki (bez nazwy pliku) , więc mogę użyć tego jako początkowego wybranego folderu .
private System.Windows.Forms.OpenFileDialog ofd;
private System.Windows.Forms.FolderBrowserDialog fbd;
...
if (ofd.ShowDialog() == DialogResult.OK)
{
string sourceFile = ofd.FileName;
string sourceFolder = ???;
}
...
fbd.SelectedPath = sourceFolder; // set initial fbd.ShowDialog() folder
if (fbd.ShowDialog() == DialogResult.OK)
{
...
}
Czy są jakieś metody. NET, aby to zrobić, Czy muszę używać regex, split, trim,
itp??
68
Author: NightOwl888, 2009-01-13
5 answers
Użyj Path
klasa z System.IO
. Zawiera przydatne wywołania do manipulowania ścieżkami plików, w tym GetDirectoryName
który robi to, co chcesz, zwracając część katalogu ścieżki pliku.
Użycie jest proste.
string directoryPath = Path.GetDirectoryName(filePath);
95
Author: Jeff Yates,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-13 14:03:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-13 14:03:06
A może tak:
string fullPath = ofd.FileName;
string fileName = ofd.SafeFileName;
string path = fullPath.Replace(fileName, "");
24
Author: Jan Macháček,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-15 12:23:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-15 12:23:15
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
strfilename = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
}
12
Author: Max,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-10 20:00:14
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-10 20:00:14
Możesz użyć FolderBrowserDialog zamiast FileDialog i pobrać ścieżkę z wyniku OK.
FolderBrowserDialog browser = new FolderBrowserDialog();
string tempPath ="";
if (browser.ShowDialog() == DialogResult.OK)
{
tempPath = browser.SelectedPath; // prints path
}
5
Author: Shaahin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-10 09:45:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-10 09:45:06
Oto prosty sposób, aby to zrobić !
string fullPath =openFileDialog1.FileName;
string directory;
directory = fullPath.Substring(0, fullPath.LastIndexOf('\\'));
0
Author: Abdel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-26 14:15:09
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-26 14:15:09