I want to copy existing Acccess Database file from my project into another location using SaveFileDialog in C#.
I wrote following code segments:
SaveFileDialog s = new SaveFileDialog();
s.Title = "SaveFile As...";
s.Filter = "Access Documents (*.accdb)|*.accdb|Others Documents (*.*)|*.*;";
string filenames = "MyAccDB.accdb";
s.FileName = filenames;
if (s.ShowDialog() != DialogResult.Cancel)
{
File.Copy(#"MyAccDB.accdb", s.FileName);
}
But it doesn't work and not saving.
How can I save access DB file into another location?
Give full path. See examples here.
File.Copy(#"C:\...\MyAccDB.accdb", #"C:\...\MyAccDB.accdb");
Just simple,
File.Copy("Source file Path","Destination file path)";
That's all.
Related
I am attempting to copy a .txt from my application directory or some kind an export feature to users desire path and filename using savedialog on C# my code is below.
private void button2_Click(object sender, EventArgs e)
{
string directory = AppDomain.CurrentDomain.BaseDirectory + "output.txt";
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "txt files (*.txt);
dialog.FilterIndex = 2;
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
File.Copy(directory, Path.GetDirectoryName(dialog.FileName) + dialog.FileName);
}
}
}
But I am getting an error
The given path's format is not supported.
I am new with C# and want to understand this error and in addition, I want to set the file name extention default as .txt also, any suggestion would be great.
There are a couple of things you need to change.
First, of course, is your copy call. This line makes no sense
File.Copy(directory, Path.GetDirectoryName(dialog.FileName) + dialog.FileName);
dialog.FileName contains already the full file name of your destination file. So there is no need to extract the directory and then add all the path again. Write just
File.Copy(directory, dialog.FileName);
But this creates a possible error. What if your user doesn't change the destination folder to another directory? You end up writing on the same file you want to read.
So I would add a sanity check like this
if(directory == dialog.FileName)
MessageBox.Show("Copy","Choose a different output folder");
else
File.Copy(directory, dialog.FileName);
Finally, if you want to force the output file to have always the .TXT extension you could add this line to the SaveDialog configuration
// Fix also your filter property. The one you have is invalid
dialog.Filter = "txt files (*.txt)|*.txt";
dialog.FilterIndex = 0; // 2 ?? There is no index 2 in your filter string
dialog.RestoreDirectory = true;
// Force the .TXT extension
dialog.AddExtension = true;
I want the user to locate a txt file on his computer which will later be used by my code for analysis. Is there a way to do that? One possible way is to make user enter the path of txt file. But that's not how I would prefer it
Thanks
string filename;
var loadDialog = new OpenFileDialog { Filter = "Text File|*.txt", InitialDirectory = #"C:\Your\Start\Directory\" };
if (loadDialog.ShowDialog() == DialogResult.OK)
filename = loadDialog.FileName;
I have tried this code it would open the file dialog to the correct location and there is only one xml file which needs to selected ( where i need to select it and click on open ) instead of selecting the file and click open to process the file is there any way to disable the open button on open file dialog. Here my xml file changes everyday. i have given *.xml but gives me a error Illegal characters in path.. my file format is this.
lborough vehicles_in 2014-06-05.xml == this changes everyday according to date.
Without clicking on open how to select the file.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "XML Files (*.xml)|*.xml";
string initPath = Path.GetFullPath("C:/Users/IT-Administrator/Desktop/LUVS/");
dialog.InitialDirectory = Path.GetFullPath(initPath);
tblVehicles = new DataTable();
dv = new DataView(tblVehicles);
if (dialog.ShowDialog() == DialogResult.OK)
{
if (dialog.FileName.Length > 0)
{
//Load Schema and Vehicle_In XML file
tblVehicles.ReadXmlSchema(Path.Combine(applicationFolder, "vehicles_in.xsd"));
tblVehicles.ReadXml(dialog.FileName);
this.dataGridView1.DataSource = tblVehicles;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.ReadOnly = true;
**Update**
I have tried this can you tell me how to open the file from Directory.get files at runtime
string[] filePaths = Directory.GetFiles(#"C:\Users\IT-Administrator\Desktop\LUVS/", "*.xml", SearchOption.AllDirectories);
FileStream stream = File.Open(#"C:\Users\IT-Administrator\Desktop\LUVS*.xml",
FileMode.Open);
tblVehicles = new DataTable();
dv = new DataView(tblVehicles);
tblVehicles.ReadXmlSchema(Path.Combine(applicationFolder, "vehicles_in.xsd"));
tblVehicles.ReadXml(stream);
Your attempted solution at the end doesn't quite get it:
/* Gives you an array of file names */
string[] filePaths = Directory.GetFiles(#"C:\Users\IT-Administrator\Desktop\LUVS/", "*.xml", SearchOption.AllDirectories);
FileStream stream = File.Open(#"C:\Users\IT-Administrator\Desktop\LUVS*.xml",
FileMode.Open);
You aren't using the array, but instead just trying to open a wildcard path; You can't do that. File.Open only accepts a single file path.
Instead, try something more like this:
/* Gives you an array of file names */
string[] filePaths = Directory.GetFiles(#"C:\Users\IT-Administrator\Desktop\LUVS/", "*.xml", SearchOption.AllDirectories);
// Work with each file individually
foreach(var filePath in filePaths)
{
using(FileStream stream = File.Open(filePath, FileMode.Open))
{
tblVehicles = new DataTable();
dv = new DataView(tblVehicles);
tblVehicles.ReadXmlSchema(Path.Combine(applicationFolder, "vehicles_in.xsd"));
tblVehicles.ReadXml(stream);
// Do whatever you need to do with the data from this one file, then move on....
{
}
Is there any reason that you can't use Directory.GetFiles to get all files in a directory and use File.Open to get the file? Why do you want to do this with a FileDialog, if you don't want the FileDialog?
Update:
//Load Schema and Vehicle_In XML file
tblVehicles.ReadXmlSchema(Path.Combine(applicationFolder, "vehicles_in.xsd"));
// Get all XML files from the files directory
string[] filePaths = Directory.GetFiles(#"files\", "*.xml", SearchOption.AllDirectories);
// Read the first XML file in the files directory
tblVehicles.ReadXml(filePaths[0]);
Is this what you asked for?
As you want to select your file and inmediately work with it, you want a button behaviour. Best way here is to make your own UserControl showing files existing on your directory.
1. Get files from directory
2. Show dialog with buttons, every buttons asociated to its file.
3. On button click, close dialog and pass file to your method.
You could use SendKeys() but it's clunky, and if the user moves focus elsewhere you might end up sending the keystrokes to the wrong window.
openFileDialog isn't very customizable, so you might want to consider using openFolderDialog and append the known filename to the user selected directory.
I'm using System.Windows.Forms.OpenFileDialog in my WPF application to select images. When user select an image, I'm displaying file name of selected file in a textbox as below.
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Select image";
fileDialog.InitialDirectory = "";
fileDialog.Filter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png";
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtImagePath.Text = fileDialog.FileName;
}
I have a button as Save in my application. When user click on this button, I need to rename this file to another name and copy it to another directory in my hard drive.
How can I achieve this?
Using File.Copy and methods in the Path class to extract the relevant part of your file
string newDir = #"D:\temp";
string curFile = Path.GetFileName(txtImagePath.Text);
string newPathToFile = Path.Combine(newDir, curFile);
File.Copy(txtImagePath.Text, newPathToFile);
Now the rename operation on the current dir using File.Move
string curDir = Path.GetDirectoryName(textImagePath.Text);
File.Move(txtImagePath.Text, Path.Combine(curDir, "NewNameForFile.txt"));
This code could be improved introducing some error handling
If you want to copy directly the old file in the new dir using the new name then you could write simply
string newPathToFile = #"D:\temp\NewNameForFile.txt";
File.Copy(txtImagePath.Text, newPathToFile);
and then do the rename on the current dir.
How about using:
System.IO.File.Copy
Alternatively, you can start a batch file, using processInfo
You can use File.Copy. Here are the details: http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx. It takes the source file and copies it to a destination, possibly under a new name.
From here I have seen it suggested that an instance of RichTextBox be created and then it's SaveFile(string path) method be used to get the file onto the hard drive:
RichTextBox rtb = new RichTextBox();
rtb.SaveFile(#"\MyFileName.rtf");
It works, but,..... is this how it should be done, I ask as it seems a little hackish? If not, then what is the correct way of doing this.
The MSDN documentation says that's exactly how you are supposed to do it.
They also have the following example here:
public void SaveMyFile()
{
// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog saveFile1 = new SaveFileDialog();
// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
// Determine if the user selected a file name from the saveFileDialog.
if(saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
// Save the contents of the RichTextBox into the file.
richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
}
}