How do I create a mandatory file save location with C#? - c#

I am working with a client application that needs to have a local repository to save updates made to a group of objects, called "Books". However, I am not able to save the objects back into the SQL server until the entire book has been updated. So the powers that be have decided to use an excel file as a repository, we are calling a work list.
The user has to be able to name the work list so that it is meaningful to them, but they cannot be able to edit the work list in excel. My idea was to create a hidden folder to store the work lists, and the app can retrieve them from that location.
Here is my question:
Is there a way to keep the windows dialog box hidden, so the user cannot choose where the file is saved to, yet pass a name to the dialog for the work
list?
I am using the standard Microsoft.Win32.SaveFileDialog currently, but I can change, if I need to, in order to accomplish this task.
Here is my SaveFileDialog code, that allows the user to change the name but also the path to where the file saves:
public void SaveToFile(string BookName)
{
var file = "";
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = string.Format("{0} \\{1} Codes", WORKLIST_SAVE_PATH, BookName); // Default file name
dlg.DefaultExt = ".xlsx"; // Default file extension
dlg.Filter = " Excel Files (.xlsx)|*.xlsx"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Save document
file = dlg.FileName;
}
else return;
File.WriteAllText(file, Export(true));
}

Related

Winforms select filename for saving document

I have some data and I would like to export it to excel. I did all the code, and everything is working fine, Now I want to save that excel file to the hard drive. I could do that too. but my problem is that I couldn't know how to allow the customer to set his/her own file name.
What I have tried:
FolderBrowserDialog brwsr = new FolderBrowserDialog();
//Check to see if the user clicked the cancel button
if (brwsr.ShowDialog() == DialogResult.Cancel)
return;
else
{
string newDirectoryPath = brwsr.SelectedPath;
//Do whatever with the new path
}
the problem of that method is it is just allows the users to select the folder that the want the file to be saved to. I want to all the user to specifiy the path and the file name.
Any idea to help pleaes?
many thanks
You need to use a SaveFileDialog instead. This let's the user specify a path + filename. Check this out for more info: SaveFileDialog on MSDN
SaveFileDialog is pretty similar to FolderBrowserDialog, so you can almost replace the existing code you already have ;)
Specify File Type (EDIT)
Have a look a the Filter property.
Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*

How to automatically select an already existing file in a directory

I am trying to replicate the standard save file process where, if there already exists a file with the necessary file extension, it is pre-selected in the SaveFileDialog. In my program I am using SaveFileDialog to allow the user to select the path for the file on the system. Upon save, it will automatically open to the path that the last saved file was, but the user has to re-select it.
Here is where I think something like this would come into play in my code:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = ".cct"; //The file extension
Nullable<bool> result = dlg.ShowDialog(); //Lets user select path
//**I'm guessing that the "pre-select" operation I am talking about
//would go here
directory = Path.GetDirectoryName(dlg.FileName); //Directory = File path on system
I have looked at the CheckFileExists property and it says that it "Gets or sets a value indicating whether a file dialog displays a warning if the user specifies a file name that does not exist." So it looks like it's more focused around checking whether or not the user enters a new file name.
How do I make it so that the previously saved file with the same extension is highlighted or selected in the SaveFileDialog if one is found upon save?
You can do a couple of things.
You could restore the path the user last saved with by setting dlg.RestoreDirectory = true.
Set the FileName before you show the dialog and it will automatically show that folder and insert the file name using dlg.FileName
http://msdn.microsoft.com/en-us/library/microsoft.win32.savefiledialog(v=vs.110).aspx

Save a file in folder opened by dialog box

My program has a treeview which lists files from a remote computer. What I need to do is to copy these files from remote into one of my local folders. I wish that when I right click the file in treeview, a dialog box shows up for me to choose a folder, and then I click "OK" in the dialog box, my clicked file could be saved inside that folder.
Since the path of the files in remote is unc path, I'm using
File.Copy(string remote_address, string local_address)
to copy the files. As i said before I need a dialog window to choose folders. So I've tried using a FolderBowserDialog, however its SelectedPath property returns me only the path to the folder not including the folder's name! And I haven't found any property to return me the folder's name.
So my questions are:
If there's a way allowing me to use FolderBowserDialog, that I could get the full path of the location where I save my file?
If there's another method allowing me to copy or download the files from remote, like using SaveFileDialog. The problem is I don't know how to us it to do this.
The following should work:
var fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() == DialogResult.OK)
{
var localPath= Path.Combine(fbd.SelectedPath, Path.GetFilename(remote_address));
File.Copy(remote_address, localPath);
}
I'm not sure which "SavePath" property you are referring to, as FolderBrowserDialog has no such property. The property you are looking for is called SelectedPath.
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.ShowDialog();
string local_address = dlg.SelectedPath;
After you call the FolderBrowserDialog's ShowDialog() method it will return a variable indicating what button the user pressed (ie, Ok or Cancel)
after you make sure the user had used "Ok" to indicate they want to proceed with the operation, you can access the "SelectedPath" field which will give you the full local path they selected.
You can then get the final path by calling
System.IO.Path.Combine(fbd.SelectedPath,remoteFileName);
I'm assuming that fbd is your FolderBrowserDialog instance and remoteFileName should contain just the filename part of the remote file (eg. "MyFile.txt");
If you want to separate the filename from the full remote path, use
var remoteFileName = System.IO.Path.GetFileName(remotePath);
That being said, what a user would typically expect is not a folder browser dialog, but the save file dialog.
you can initialize the save file dialog with a filename, leaving the user to select a folder and possibly change the intended file name as well if they wish.
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = remoteFileName;
sfd.ShowDialog();
sfd.FileName // now contains the full path to the file that the user has selected
don't forget to take the result from the ShowDialog() call to make sure the user didn't cancel out of the save dailog!

Writing a JSON (or TXT) file in WPF C#

I have been trying to make sense of this http://msdn.microsoft.com/en-us/library/sfezx97z.aspx which uses the SaveFileDialog, but it is hard for me to understand. I have the following code:
FileInfo existingFile = new FileInfo("C:\\Users\\cle1394\\Desktop\\Apple Foreign Tax Payment Sample Layout Proposed - Sample Data.xlsx");
ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile);
var json = new JavaScriptSerializer().Serialize(data);
How can I output the contents of json to a .json or .txt file?
I would like to let the user either see a link/ button to click to download/ save the file to a location on their computer, or, simply display the save file dialog box so that they can save the file to a location on their computer.
EDIT (to let OP comment on what parts are not clear):
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.ShowDialog();
if(saveFileDialog1.FileName != "")
{
File.WriteAllText(saveFileDialog1.FileName,json);
}
You are looking for this, then:
File.WriteAllText(#"c:\some\path\json.txt",json);
And note that it will save the file using UTF8-encoding without a Byte Order Mark. If you need the BOM, you need to use the File.WriteAllText(path, content, Enconding);
See here.
Update - adding sample with SaveFileDialog:
if(!string.IsNullOrEmpty(saveFileDialog.FileName))
{
//saveFileDialog.FileName should contain the full path
//according to the documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filename.aspx
File.WriteAllText(saveFileDialog.FileName,json);
}

WPF copy file to the predefined directory

In a WPF app I need to make it possible for a user to pick a file by the standard Open File Dialog and save it to the predefined folder (user doesn't know where it is) right after the user click OK button on Open File Dialog. Something like of importing a file to the application. I do it in the following way:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.SafeFileName;
System.IO.File.Copy(filename, #"E:\TestFolder\" + filename);
MessageBox.Show("File " + filename + " saved");
}
Is there a standard way to check if the file already exists before trying to save it and if it is really saved after saving it?
Look at System.File.Exists that should be able to tell you what you need to know.
The System.IO.File.Exists method returns true if a file at the given path exists, so you could use it to check both before and after your copy operation.
Use the SaveFileDialog (Microsoft.Win32). If you try to save over a file that already exists it will prompt you to make sure you want to save over that file. This doesn'y actually save it though, all it will do is provide the name and location of the file you want to create/save over. After you use the SaveFileDialog to select the file you then need to do the work of saving the file.
This post may be helpful

Categories

Resources