SaveDialog file exist? - c#

I use this code to save an avi file. When I create a new file, it's no problem.
But When I choose an existing file, It does not work and saveFileDialog still shows.
I have set saveDialog.OverwritePrompt and saveDialog.CheckFileExists is true, but it is not ok. If I set saveDialog.OverwritePrompt is false it runs, but it does show overwrite warning
How can I solve this?
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.OverwritePrompt = true;
DialogResult dgResult = saveDialog.ShowDialog();
if (dgResult == DialogResult.OK)
{
exportAvi(saveDialog.FileName);
}

This code works - if I choose to overwerite an existing file it shows me the Prompt:
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.OverwritePrompt = true;
DialogResult dgResult = saveDialog.ShowDialog();
if (dgResult == DialogResult.OK)
{
//exportAvi(saveDialog.FileName);
}
[Window Title]
Confirm Save As
[Content]
XYZ.txt already exists.
Do you want to replace it?
[Yes] [No]

Your issue is in the exportAvi() function. You are not allowing for the file to be overwritten. I cannot see your exportAvi() function so I cannot tell you what is wrong exactly. If you post your exportAvi() function I can help you further.
saveDialog.OverwritePrompt - will only prompt the user if they want to overwrite. It will not overwrite the file. You have to handle this in your code.
saveDialog.CheckFileExists - will only check if the file exists.
Check your export function and make sure you are setting the overwrite parameter to True.

Related

Export text to a browsed file in wpf

I'm trying to export some text to a file browsed by the user and create it if this file does not exists.
What I've done till now is to use OpenFileDialog but I don't know if it's the right way to do it and also if it is I don't know how to continue
You can use the SaveFileDialog()
var dialog = new SaveFileDialog();
dialog.ShowDialog();
var path = dialog.FileName;
File.WriteAllText(path, "YourData");

InitialDirectory not working

VERY VERY new to C Sharp, as it's not part of my study path, but I have to edit small codes in C Sharp in order for my app to work. I'm using Ogama, a open source gaze tracker, which I need for my project. The heatmap, to be more specific. Now, I want to save the heatmap to a directed folder, and managed to find the code. The initial code was
public static bool ExportImageToFile(Image image)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "Please enter filename for image...";
dlg.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
So I thought I could change it, by following other tutorials online, and this was my code.
public static bool ExportImageToFile(Image image)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "Please enter filename for image...";
dlg.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "heatmapimages");
But it doesn't work. When I click the save file button, it brings me to where my Ogama projects are. The 'heatmapimages' folder is in my Desktop. Any advice? Thank you in advance.
EDIT: Managed to make it work, by changing Special.Personal to Special.DesktopDirectory. Is there a way to make it autosave the image? Such that I don't need to click save?
You need to set RestoreDirectory true and set InitialDirectory to SpecialFolder.Desktop. Here are all SpecialFolders for your reference.
It restores the current directory to its original value if the user changed the directory while searching for files.

Bug in C# OpenFileDialog if two fullstops in filename

I have an OpenFileDialog with the filter set to *.wav. However, when I execute the OpenFileDialog it also shows other files that includes .wav but the true extension is not .wav but e.g. png. Why is that and how can I avoid this?
Right now I take care of it when loading the file(s) for processing but I would like to avoid getting them in the OpenFileDialog list in the first place. Is this a bug in the control or is it me?
Background: I had by accident renamed a picture file to TheFile.wav.png - stupid, true, but these kind of things happens also for other users.
Thanks in advance
Try this to set the file type in the dialog:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "wav files (*.wav)|*.wav";
dialog.InitialDirectory = #"C:\"; // You may not need this.
if (dialog.ShowDialog() == DialogResult.OK) // Or this; I was just being thorough.
{
// Your code can go here.
}
Just make sure that when using the .Filter property, you follow the pattern I have above, or else it won't work. Also, as was mentioned above, you may want to do some validation after the user selects something.

Create, write to and open a text file from SaveFileDialog

I am displaying a SaveFileDialog and when OK is clicked I am creating new file, writing some default content to it and then attempting to Open it via the OpenFile() method of the SaveFileDialog. However, the moment I call OpenFile() the content of the file are deleted.
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML files (*.xml)|*.xml";
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// First Event Creates file and writes default content to it - works ok
NewFileCreated( this, new FileCreatedEventArgs() { Template = Template.BBMF, FilePath = saveFileDialog.FileName } );
// Second Event clears file content as soon as saveFileDialog.OpenFile() called
FileLoaded( this, new FileLoadedEventArgs() { FileStream = saveFileDialog.OpenFile() } );
}
Can someone explain why this happens and what I need to be doing to successfully Open the newly created file?
According to MSDN, SaveFileDialog.OpenFile()
Caution
For security purposes, this method creates a new file with the
selected name and opens it with read/write permissions. This can cause
unintentional loss of data if you select an existing file to save to.
To save data to an existing file while retaining existing data, use
the File class to open the file using the file name returned in the
FileName property.

return a filename

using openFileDialog will not return a filename in use, I want the filename only I don't care if it's in use
The file will most likely be in use, I just want to be able to find the file and retrieve its name and location to perform a connection.
OpenFileDialog works until I select the file, then it has a popup that says "File in Use". I don't want it to check for that, just return the filename.
It seems that setting the ValidateNames property to false solves the problem (but don't ask why :) ...)
Here's the code I used to try out:
var f=File.OpenWrite(#"C:\test.txt");
var ofd = new OpenFileDialog();
ofd.ValidateNames = false;
ofd.ShowDialog();
f.Close();
Commenting out the third line gave me the described error "file in use".
Try setting ValidateNames to false.
OpenFileDialog fd = new OpenFileDialog();
fd.ValidateNames = false;
I googled and found a thread that suggests this is a bug in the control:
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56fbbf9b-31d5-4e89-be85-83d9cb1d538c/
The suggested workaround is to call the API directly as found here:
http://www.codeproject.com/KB/dialog/customize_dialog.aspx?print=true

Categories