I am developing a WPF app and I just came across Loading a Script File(.xml) operation where I should load it from the system, get the file from the combobox and call Load Method.
Xaml:
<ComboBox Name="ScriptCombo" SelectedIndex="0" >
<ComboBoxItem Content="Select Aardvark Script" />
<ComboBoxItem Content="{Binding ScriptPath}" />
</ComboBox>
<Button Content="..." Command="{Binding Path=ScriptPathCommand}" Name="ScriptFileDialog" />
ViewModel:
private string _ScriptPath;
public string ScriptPath
{
get { return _ScriptPath; }
set
{
_ScriptPath = value;
NotifyPropertyChanged("ScriptPath");
}
}
// Method gets called when ... Button is clicked
private void ExecuteScriptFileDialog()
{
var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
dialog.DefaultExt = ".xml";
dialog.Filter = "XML Files (*.xml)|*.xml";
dialog.ShowDialog();
ScriptPath = dialog.FileName; //Stores the FileName in ScriptPath
}
This opens a File dialog and lets me select a .xml file. Here it doesnt show me the CurrentWorkingDirectory when Dialog opens. How can that be achieved??? Thus After Selecting when I click Open and put a breakpoint near ScriptPath statemnt, it shows the path of the file in my combobox.
Also I want to get this file and store it in a FILE type and thus call LoadFile method. I did it in C++ as follows:
File file = m_selectScript->getCurrentFile(); //m_selectScript is combobox name
if(file.exists())
{
LoadAardvarkScript(file);
}
void LoadAardvarkScript(File file)
{
}
In WPf i did like :
ScriptPath = dialog.FileName;
if (File.Exists(ScriptPath))
{
LoadAardvarkScript(ScriptPath);
}
}
public void LoadAardvarkScript(string ScriptPath)
{
MessageBox.Show("Start Reading File");
}
I am passing FILE as perameter in C++ code and here I am passing a string. Will it create any issue while reading the xml file?
I don't exactly understand what your problem is, but the directory that is initially shown by an OpenFileDialog is set by its
InitialDirectory
property and hence is what you have put into yourdefaultPath
variable. This could for example be the value of the System.Environment.CurrentDirectory property.For the second part of your question, there is a File class in .Net.