dotnet / dotnet/dotnet-api-docs
Behaviour when set to empty string is not specified
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
FIleDialog has interesting results if InitialDirectory is set to "". It seems (by experiment on Windows 7 with .NET 4.7.2) that the initial directory is set to the last directory searched for the specified file type. This can have odd repercussions if two programs running at the same time both set InitialDirectory to "" - changing the directory in one program changes it for the other one! I would suggest adding some text warning users to be aware of this, and not to set InitialDirectory to "" unless they really want to.
There has been quite a lot of discussion of this on StackOverflow, so it's obviously an issue. A bit of documentation would help.
I have not been able to test this on other version of Windows of .NET.
A simple VB program follows - run two copies of it and watch them interfere!
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
Dim S As String = ""
Dim DR As DialogResult
With OFD
.InitialDirectory = S
.Filter = "Text files (*.txt)|*.txt|all files|*.*"
.FilterIndex = 1
.Title = "Find a file"
DR = .ShowDialog()
If DR <> DialogResult.Cancel Then
S = .FileName
MsgBox("File name is:" & S)
If Not S.Equals("") Then
S = Path.GetDirectoryName(S)
MsgBox("Directory is: " & S)
End If
End If
End With
End Sub
End Class
---
#### Document Details
⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*
* ID: 5c39987f-9d55-32be-30ee-cd5cf1e04e6a
* Version Independent ID: e9365a8b-a4c9-d5e0-3b34-ddb965df7b8a
* Content: [FileDialog.InitialDirectory Property (System.Windows.Forms)](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.initialdirectory?view=netframework-4.7.1#System_Windows_Forms_FileDialog_InitialDirectory)
* Content Source: [xml/System.Windows.Forms/FileDialog.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Windows.Forms/FileDialog.xml)
* Product: **dotnet-api**
* GitHub Login: @dotnet-bot
* Microsoft Alias: **dotnetcontent**
Contributor guide
Assessment
This issue has not been assessed yet.