I am using microsoft.win32.savefiledialog to save a file inside a folder. Only in windows XP, after saving file in a folder (ex: abc) , I cannot delete abc folder. Error message displays saying that another process is using this. Seems like handles are remaining on selected folder. Please give me a solution on this.
Following is my save file dialog code:
SaveFileDialog fileDialog = new SaveFileDialog();
fileDialog.DefaultExt = !string.IsNullOrEmpty(this.DefaultExtension) ? this.DefaultExtension : "*.*";
fileDialog.Filter = !string.IsNullOrEmpty(Filter) ? Filter : "All Files|*.*";
fileDialog.FileName = !string.IsNullOrEmpty(this.FileName) ? this.FileName : string.Empty;
fileDialog.InitialDirectory = !string.IsNullOrEmpty(this.DefaultPath) ? this.DefaultPath : string.Empty;
if (fileDialog.ShowDialog().Value == true)
{
fileName = fileDialog.FileName;
}
else
{
fileName = string.Empty;
}
return fileName;
EDITED :
This is common for System.Windows.Forms also, I tried lot, issue happnes when I select a folder from file dialog window. no need to do anything after that, just select a folder form save file dialog. that foldercannot be deleted .
This is entirely normal. It is not another process that has the directory object opened, it is your process. Your code made the directory the default working directory of your process. Something you can see from the Environment.CurrentDirectory property.
Set the SaveFileDialog.RestoreDirectory property to true to avoid this.
Related
I am creating an installer and i would like to know how to use SaveFileDialog to get the file path where the user wants to install their mod to. The user should be able to click a button to open up the dialog, navigate to the folder and click select folder and once done is clicked have it stored as {DownloadPath}.
Try looking at the documentation
There is also a tutorial on this.
var filePath = string.Empty;
var saveFileDialog = new SaveFileDialog
{
Filter = #"Exe Files (.exe)|*.exe|All Files (*.*)|*.*"
};
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != string.Empty)
{
filePath = saveFileDialog.FileName;
}
follwing simple code:
Microsoft.Win32.OpenFileDialog _fileDialog = new Microsoft.Win32.OpenFileDialog();
_fileDialog.Title = CarPlus.Base.Properties.Resources.ContentSettingsDatabase_FileOpen_TitleRestore;
_fileDialog.CheckPathExists = false;
_fileDialog.CheckFileExists = false;
_fileDialog.ValidateNames = false;
_fileDialog.Multiselect = false;
_fileDialog.InitialDirectory = _searchPath == null ? System.Environment.SpecialFolder.MyComputer.ToString() : _searchPath;
_fileDialog.Filter = "Wiederherstellungsarchiv (*.fbk)|*.fbk";
// Display OpenFileDialog
if (_fileDialog.ShowDialog() == true)
return (_fileDialog.FileName);
return (null);
the sense is to give the user the possibility to select a file or to write down his own path and file name. that is necessary, because the selected file is a database backup file for a firebird instance. the firebird server will use this file to restore the database but he mus have access to that file and of course, if the firebird instance is running on another computer the file path can be different.
my problem is, if i write down in the dialog 'c:\bar.fbk' is everything fine even when the file not exist on the local computer, but if i write 'c:\foo\bar.fbk' i will get an error message that the path not exist.
but why? my filedialog setup includes
_fileDialog.CheckPathExists = false;
who knows why?
I have recently started to get an error which states my directory can not be found I have tried a number of ways to solve this but have yet to find a solution.
The method should allow the user to select an image for their computer and add it to a folder called images inside the applications folder structure. The problem is that when using the File.copy(imageFilename, path); it throws the error. I have tried changing the path and you will see from the code snip it. It is even doing it when the program itself has passed the file path for the application and is still throwing me the error.
this is the method.
private void btnImageUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog imageFile = new OpenFileDialog();
imageFile.InitialDirectory = #"C:\";
imageFile.Filter = "Image Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
imageFile.FilterIndex = 1;
if (imageFile.ShowDialog() == true)
{
if(imageFile.CheckFileExists)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
System.IO.File.Copy(imageFile.FileName, path);
}
}
}
I am using VS2013 and have included the using Microsoft.win32
Any further information needed please ask.
Thanks
There are 2 things need to be taken into consideration
private void btnImageUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog imageFile = new OpenFileDialog();
imageFile.InitialDirectory = #"C:\";
imageFile.Filter = "Image Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
imageFile.FilterIndex = 1;
if (imageFile.ShowDialog() == true)
{
if(imageFile.CheckFileExists)
{
string path = AppDomain.CurrentDomain.BaseDirectory; // You wont need it
System.IO.File.Copy(imageFile.FileName, path); // Copy Needs Source File Name and Destination File Name
}
}
}
string path = AppDomain.CurrentDomain.BaseDirectory; You won need this because the default directory is your current directory where your program is running.
Secondly
System.IO.File.Copy(imageFile.FileName, path); Copy Needs Source File Name and Destination File Name so you just need to give the file name instead of path
so your updated code will be
private void btnImageUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog imageFile = new OpenFileDialog();
imageFile.InitialDirectory = #"C:\";
imageFile.Filter = "Image Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
imageFile.FilterIndex = 1;
if (imageFile.ShowDialog() == true)
{
if(imageFile.CheckFileExists)
{
System.IO.File.Copy(imageFile.FileName, SomeName + ".jpg"); // SomeName Must change everytime like ID or something
}
}
}
I'm not sure if that's the problem, but the File.Copy method expects a source file name and a target file name, not a source file name and directory: https://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspx
So, to make this work, in your case you'd have to do something like the following (namespaces omitted):
File.Copy(imageFile.FileName, Path.Combine(path, Path.GetFileName(imageFile.FileName));
Note that this will fail if the destination file exists, to overwrite it, you need to add an extra parameter to the Copy method (true).
EDIT:
Just a note, the OpenFileDialog.CheckFileExists does not return a value indicating if the selected file exists. Instead, it is a value indicating whether a file dialog displays a warning if the user specifies a file name that does not exist. So instead of checking this property after the dialog is closed, you should set it to true before you open it (https://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.checkfileexists(v=vs.110).aspx)
When using a WinForm to click a button. and call save filDialog will come out
"C:\Windows\system32\config\systemprofile\Desktop".
http://i.stack.imgur.com/wH7J9.jpg
The challancing is this software must be runing in the "System" user.
But I using the SaveFileDialog for saving a file. will comes out the message like "C:\Windows\system32\config\systemprofile\Desktop".
I guess the problem is that when I using "system" user, the default user profile is no exist and counld no find the "Destop" floder path.
Because it will not have the "user profile" # "system" user.
This error comes out at I click the button, this application try to init the SaveFileDialog and also try genarate the icon shortcut on the left hand side so cause the error.
The dropdownbox also have the save problem.
http://i.stack.imgur.com/5Hy3S.jpg
Could any one know how to remove the icon shourt cut at the left hand side and the dropdown box icon also have the same problem.
using (var dialog = new SaveFileDialog())
{
dialog.DefaultExt = "txt";
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.Title = "test";
dialog.AutoUpgradeEnabled = false;
dialog.InitialDirectory = Application.StartupPath;
try
{
DialogResult result = dialog.ShowDialog(this);
if (result == DialogResult.OK)
{
//do something
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
}
I just using very very very simple code.
ps. I am using C#.net 4.0 , vs2010 running at win2008r2 and win 7
Thanks all !!!
Try setting the InitialDirectory property of the SaveFileDialog
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = "c:\\yourDirectory";
You can also use this method to check if the my documents directory exists on disk.
Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
I'm using the CommonOpenFileDialog in the Windows API Code Pack as a folder picker dialog. I'm setting the InitialDirectory property to Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments). However, when I display the dialog, the path in the address bar is Libraries\Documents (not C:\users\craig\my documents as I'd expect). Additionally, if I just press the Select Folder button, I get a dialog saying that 'You've selected a library. Please choose a folder instead.'
Does someone know why my file path is being ignored, in favor of 'libraries\documents'? More importantly, how can I get the dialog to respect the InitialDirectory value I passed in?
The code I'm using for the dialog is:
if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = true;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.AllowNonFileSystemItems = false;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folderSelectorDialog.Title = "Project Location";
if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
ShellContainer shellContainer = null;
try
{
// Try to get a valid selected item
shellContainer = folderSelectorDialog.FileAsShellObject as ShellContainer;
}
catch
{
MessageBox.Show("Could not create a ShellObject from the selected item");
}
FilePath = shellContainer != null ? shellContainer.ParsingName : string.Empty;
}
}
Thanks,
-Craig
First of all, I'm sorry it took me so long to understand your question.
The message I see is when I try this is:
Cannot operate on
'Libraries\Documents' because it is
not part of the file system.
There's not much more to say. A library is a virtual folder that is an amalgamation of various different real folders.
There's no real way to avoid this error. You have asked the dialog to return a folder and the user has not selected a folder. The dialog therefore cannot fulfil its part of the deal.
If you descend further into the folder structure, into real folders, then the dialog will return you a real value.
Instead of
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
try
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);