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.
Related
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");
I was just at Open directory dialog, and they said "get this package, and do this and this to get a folder select window to show up". Well, that all works great, using the Windows API Code Pack-Shell package. However, now I want to get the actual folder that is selected. I didn't notice them mentioning this anywhere.
I tried to do string folderLocation = Convert.ToString(dialog); (dialog is the variable for opening the folder window), but that only gave me like the property of the variable. I also tried this: CommonFileDialogResult result = dialog.ShowDialog();
string folderLocation = Convert.ToString(result);
But that just gave me "Ok" - which I take it is the result of it, instead of the actual folder.
The result of ShowDialog just indicates wether the user clicked OK, cancel, or just closed the window.
CommonOpenFileDialog can be used for both files and folders, so it's a bit surprising when used as a folder picker, but the path is stored in FileName.
var dlg = new CommonOpenFileDialog();
dlg.IsFolderPicker = true;
if(dlg.ShowDialog() == CommonFileDialogResult.Ok) {
Console.WriteLine(dlg.FileName);
}
If I understood correctly, you want to get Folder for a selected file? If that's the case, you can take FileInfo for that file, and extract folfer from it. Like this:
System.IO.FileInfo fInfo = new System.IO.FileInfo(oFD1.FileName);
MessageBox.Show(fInfo.DirectoryName);
PS. oFD1 is OpenFileDialog
I have a problem where if you set the filename in the dialog box to a sub directory within the initial directory you set it to and then clicking 'Save', the dialog window doesn't actually save the file but opens the sub directory which I could still interact with.
For example If I set the initial directory for the dialog to 'C:\MainDir' and that directory consists of SubDir1, SubDir2, then in the save file dialog I could see that I am in the initial directory with two sub directories. If I set the filename to SubDir1 (no extension) in the dialog, and then I hit 'Save', what happens is instead of saving the file as 'filename.extension' the dialog opens the directory specified by the file name.
Here's what I currently have:
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = ext;
dlg.AddExtension = true;
dlg.FileName = filename;
dlg.Filter = filter;
dlg.FileOk += OnFileDialogOk;
dlg.InitialDirectory = dir;
bool? dlgRes = dlg.ShowDialog();
Is this something that can be easily fixed?
Quick Answer: No.
You cannot override the default save method of Windows OS.
What you can do is perhaps to verify whether the filename you wanted to use (in this instance, SubDir) exists already as a directory. If it does, then you would need to change that name, as that will only manifest the behavior you've already seen.
Side Note: Just imagine you have a very important folder which contains critical files, and Windows would let you save a file that is named with that directory. That is a disaster waiting to happen.
The only ways I can think of doing this are a bit extreme:
You could roll your own dialog
You could modify the functionality of the standard dialog
The answers found here: Customizing OpenFileDialog could help with that.
I guess I should also note that while it may seem helpful to accommodate this kind of input and automatically append the extension, it'll be counter-intuitive to many users who will expect the default behaviour.
In short, I'd probably think twice about this.
I'm trying to save a bitmap to a file, all the examples and tutorials I have found suggest using this line of code to do so-
private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) // Save the fractal image
{
SaveFileDialog dialog = new SaveFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
fractal.Save("myfile.png", ImageFormat.Png);
}
}
When I execute the code by click the save image button (which calls the above method) a save dialog appears but both the file name field and file type field are empty. I select a location to save to and give the file a name - e.g. bitmap.png then check the location and nothing has saved.
I have also checked the debug folder and nothing has appeared there ether.
I'm assuming I'm not far off or that I've made a silly mistake elsewhere any ideas or suggestions?
Assuming you are using the SaveFileDialog class, you need to set the Filter and DefaultExt properties to get the file extensions to show up.
You then read the FileName property as the argument to your Save() call
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.