Get file path stored to string var with SaveFileDialog C# WPF - c#

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;
}

Related

Is there a way to get the icon of a shortcut link?

I'm trying to open an application by using it's shortcut file in C#.
right now I am able to get it's path but I Wanted to know if I can get it's icon too or not?
(I mean the icon of the application which is located in it's main folder not the icon of it's shortcut)
thank you.
Right Now My Code is like this:
public static void DoSomeHeavyLifting()
{
string filepath = #"C:\Users\Hello\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Adobe Photoshop 2020.lnk";
string pathOnly = filepath;
string filenameOnly = filepath;
Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
Console.WriteLine(link.Path);
}
}
as you see I can Only Have link.Path But I want it's icon too
Does this work for you?
It's pretty straightforward to use just with the file path.
private void ExtractAssociatedIconEx()
{
Icon ico =
Icon.ExtractAssociatedIcon(#"C:\WINDOWS\system32\notepad.exe");
this.Icon = ico;
}
Just make sure the file exists first as you will get ArgumentException if it doesn't

Directory could not be found

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)

microsoft.win32.savefiledialog issue in windows xp

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.

C# SaveFileDialog come out warning when using "System" user to running the WinForm

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));

How can I get the CommonOpenFileDialog's InitialDirectory to be the user's MyDocuments path, instead of Libraries\Documents?

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);

Categories