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
Related
I'm trying go get a path to a 'busy' file using openfiledialog in winforms and C#. Using the following code:
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
this.dbPath = this.textBox1.Text;
}
}
When I point to the file in question I get the following error:
Error Message:
MyDbContext.mdf
This file is in use.
Enter a new name or close the file that's open in another program.
Which is fine, because I already know the file is in use, all I want is to store the file path into a string, without opening it.
Perhaps openfiledalog is the wrong option here, after all I don't want to open the file, only to list it's path. However I didn't find anything else in winforms that points to a file.
Is there any other means to achieve what I want?
Try setting ValidateNames to false before ShowDialog()
openFileDialog1.ValidateNames = false;
Hi I created a wpf application in C# using Access as the db. I am trying to create a backup where the user will click on the backup button and then the file explorer window will open so that the user can choose where they want to save their backup. I looked on the site and there are several topics on this but I did not see one where the file explorer was called. I know how to create a backup copy using the following:
private void BackupDatabase_Click(object sender, RoutedEventArgs e)
{
File.Copy("Results_West.mdb", "Results_WestBak.mdb", true);
}
The above works and saves the backup where the executable is located but I'm not sure how to call windows file explorer and then do the save where the user wants to like when you're saving other files. Any ideas on this would be helpful.
Thanks.
You should use a SaveFileDialog located under the Microsoft.Win32 namespace.
The SaveFileDialog doesn't actually do any saving work, it's just a dialog to help you get a destination file path.
SaveFileDialog.ShowDialog() will show the dialog, and when the user closes the dialog (eg. by accepting or declining), it will return a nullable boolean value to indicate if the dialog was accepted, declined or cancelled.
If ShowDialog() returns true, you should have a file path (string) that you can use in your copy method.
I hope this helps you understand. I have attached some code to help.
public void BackupDatabase()
{
// The relative or full path of the database that you want to copy
string fileToBackup = "Results_West.mdb";
// The directory the save file dialog opens by default
// --- Optional ---
string initialFilePath = #"C:\Backup";
// Initialize the sfd
SaveFileDialog sfd = new SaveFileDialog
{
Title = "Choose a destination for your backup.",
Filter = "Microsoft Access File|*.mdb",
// --- Optional ---
InitialDirectory = initialFilePath,
};
// sfd.ShowDialog() returns a nullable bool
// If it returns true, you should have enough information to work with.
// We need to escape if the result is not true.
if (sfd.ShowDialog() != true)
{
return;
}
try
{
// sfd.FileName is the full path that the user selected.
// 3rd parameter (true) specifies overwrite
File.Copy(fileToBackup, sfd.FileName, true);
}
catch
{
// Failed to copy file.
}
}
I have this web application project which requires a better user-interface and I am doing it by C#.
I already have my html file with JS done but I need some data from user.
In my JS embedded in HTML file, I used the code below to find the file on local driver and get the data from that excel file and then put all these data into an array.
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open(file1);
var excel_sheet = excel.Worksheets("Sheet1");
However, the "file1" you see above seems to require a full name path,say "C:\test.xls" in my case.
I am new to C# and just built a button on my form design, by clicking the button, I seem to be able to browse my local file.
private void button1_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
System.Diagnostics.Process.Start(file);
}
Console.WriteLine(size); // <-- Shows file size in debugging mode.
Console.WriteLine(result); // <-- For debugging use.
}
So, my question:
How can I get this kind of full file path of an uploaded file in C# ?
And furthermore, it would be awesome if some one can tell me how to get this value into my javascript or HTML!
Thank you in advance
You won't be able to depend on getting the full path. In the end, the browser only needs to multi-part encode the physical file and submit it as a form post (of sorts). So once it has it's location and has encoded it -- it's done using it.
It's considered a security risk to expose the file structure to Javascript/HTML (ie., the internet).
Just an update.
I used another logic and it worked as expected. Instead of getting absolute file path , I managed to open the file , save it as somewhere and make my html call find that file path no matter what.
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.
I am importing a source file and processing it and then after that I have to save it in a new location. I have created the syntax for importing file, tell me the syntax to save it to a new location. One is when I am calling the constructor to give the path of import file, then I can also give the path for the output location. But don't know how to implement it. Please tell.
You can use a SaveFileDialog pretty much like this:
using ( var dlg = new SaveFileDialog() )
{
if ( dlg.ShowDialog() == DialogResult.OK )
{
//SAVE THE OUTPUT
//DEPENDING ON THE FORMAT, YOU MAY WANT TO USE
//File.WriteAllBytes(dlg.FileName, yourBytes);
//File.WriteAllText(dlg.FileName, yourText);
//File.WriteAllLines(dlg.FileName, yourStringArr);
//OR ANY OTHER CODE YOU WANT TO USE TO PERSIST YOUR DATA
}
//else the user clicked Cancel
}
Also, you can set a default extension, a default path and more. Look up SaveFileDialog's information on MSDN
As your importing the file you could use the StreamWriter base class to write it to a file that the end user designates in a either a text box or a file upload box.