I have a problem. I have created a temporary .csv file, which contains a lot of data. The directory is a string: path = #"C:\Users\xxxxx\Documents\TempFile.csv";.
I want to save this temporary file as a .csv file, when the user click on the save button. I have created a dialog, so the user is able to choose where to save the file.
private void SaveBT_Click_1(object sender, EventArgs e)
{
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "CSV File|*.csv";
saveDialog.Title = "Gem en CSV fil";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
}
}
}
erm, how about?
File.Copy(tempFileName, saveFialog.FileName);
Related
Is it possible to store the files that you have opened using OpenFileDialog in WPF? Currently, I have this code that opens a file from the computer and shows the directory of it in a listBox:
private void UploadEmployeeRank1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
foreach (string filename in openFileDialog.FileNames)
employeeRank1PrivateMaterialsListBox.Items.Add(filename);
}
}
However, once I close the application and open in again, the file that I have loaded in the listBox is gone. How do I make it stay?
OpenFileDialog does not actually load the files. It returns you the path of the selected files. So at the end you will just need to save those strings.
One simple and quick way is to write them in local file.
Here are two functions that will save and load file paths in a file.
private void SaveFiles()
{
if (listBox1.Items.Count > 0)
{
StringBuilder files = new StringBuilder();
foreach (var file in listBox1.Items)
{
files.AppendLine(file.ToString());
}
File.WriteAllText("myfiles.txt", files.ToString());
}
}
private void LoadFiles()
{
if (File.Exists("myfiles.txt"))
{
string[] files = File.ReadAllLines("myfiles.txt");
listBox1.Items.AddRange(files);
}
}
SaveFiles() function loads the files from your listbox and saves each path as a new line in text file.
LoadFiles() function parses the file and loads the data in your listbox.
Good day.
I have a Windows forms application (C#).
With the "open file dialog" I can select a .pdf file.
Selected .pdf file is copied and stored in a pre-determined destination.
File path of selected .pdf file is stored in an SQL database.
What is the function/method to open a .pdf file (in axAcroPDF...), which is stored in a folder and its file path is stored in a database?
This is what I have, code vise:
private void txtST1Cap_DoubleClick(object sender, EventArgs e)
{
SavedDocumentPath1 = #"XXX\";
using (OpenFileDialog OpenFileDialog1 = new OpenFileDialog() { ValidateNames = true, Multiselect = false, Filter = "PDF|*.pdf" })
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//display PDF in reader
OpenedDocument1 = openFileDialog1.FileName;
axAcroPDF1ST1.src = openFileDialog1.FileName;
//code for getting REF No. from opened file name
OpenedDocumentREF = Path.GetFileName(openFileDialog1.FileName);
REFfromOpenedDocument = OpenedDocumentREF.Substring(0, 12);
txtST1Cap.Text = REFfromOpenedDocument;
//destination of to-be saved document
SavedDocLoc1 = (SavedDocumentPath1 + Path.GetFileName(openFileDialog1.FileName));
lblST1CapLocation.Text = SavedDocLoc1;
}
}
private void btnST1Cap_Click(object sender, EventArgs e)
{
openFileDialog1.FileName=SavedDocLoc1;
axAcroPDF1ST1.src = openFileDialog1.FileName;
}
Button Clicl btnST1Cap does not work.
Thank you.
I was almost correct.
So, if one wants to open a PDF document in a "default program" System.Diagnostics.Process,... is used:
private void btnST1Cap_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(lblST1CapLocation.Text);
}
As mentioned, this opens a default .pdf browser.
I wanted, to open it in my form (in axAcroPDF...):
private void btnST1Cap_Click(object sender, EventArgs e)
{
axAcroPDF1ST1.src = lblST1CapLocation.Text;
}
Yaaaay 4 ME!
In above cases "lblST1CapLocation.Text" is the label where file path is stored.
Direct file path can be inserted if wished:
System.Diagnostics.Process.Start(#"c:\myPdf.pdf");
I have created some code that saves my work to a text file, I was just wondering is there some way so that when I click 'Save' I can read in the text from richTextBox1and set it as the default file name, still with the 'txt' default file extension.
e.g. When I click 'save' the folder dialog comes up and asks you to name your file, just as it would if you were using Word for example, I want that box to already have the text from my richTextBox1 in.
Thanks.
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "C:\\To-Do-List";
save.Filter = "Text Files (*.txt)|*.txt";
save.DefaultExt = ".txt";
DialogResult result = save.ShowDialog();
if (result == DialogResult.OK)
{
using (StreamWriter SW = new StreamWriter(save.FileName))
{
SW.WriteLine(richTextBox1.Text);
SW.WriteLine(richTextBox2.Text);
SW.WriteLine(richTextBox3.Text);
SW.WriteLine(richTextBox4.Text);
SW.WriteLine(richTextBox5.Text);
SW.Close();
}
}
Just set the FileName property on your SaveFileDialog.
Add
save.FileName = String.Format("{0}.txt", richTextBox1.Text);
Before you call ShowDialog.
This is the code:
private void beginOperationToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFileNameToolStripMenuItem.Enabled = false;
if (File.Exists(fullDefaultDirectory))
{
File.Delete(fullDefaultDirectory);
}
ffmp.Start(fullDefaultDirectory, 25);//"test.avi", #"d:\", 25);
timer1.Enabled = true;
startStop = true;
}
Now i check if the file exist and delete it but thats not good way since i need a file to be on the hard disk.
So what i want to do is:
If the fie exist ask the user if to delete it or run over it with the same name.
If deleted open the savedialog and let the user to set a new file name.
If not deleted just run over the existing file and create the same file name.
This is the savedialog i already have:
private void changeFileNameToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Avi|*.avi";
saveFileDialog1.Title = "Save an Avi File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
//outputFileName = Path.GetFileName(saveFileDialog1.FileName);
//outputDirectory = Path.GetDirectoryName(saveFileDialog1.FileName);
fullDefaultDirectory = saveFileDialog1.FileName;
Options_DB.Set_Video_File(fullDefaultDirectory);
}
}
But im talking about a situation when the user didnt change anything and the variable fullDefaultDirectory contain the same directory and file name then let the user to decide if to delete or run over it .
fullDefaultDirectory always contain a file name since i have a settings file name text file where i save the directory and file name the user selected.
If he didnt select anything the default is some file i did to be default and if he selected a file it will create the file the user selected.
I need to solve the case where the file is already exist.
The SaveFileDialog class already covers this with the OverwritePrompt property, which will cause the dialog to ask the user if they're sure they want to overwrite an existing file.
I have a quick question, how do you save a file in a different format like in "save as"
so far i got this
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
//this saves the file as a text or richtext.
saveFileDialog1.Filter = ("RichText*.rtf; )|*.rtf; |TextDocs *.txt;|*.txt");
saveFileDialog1.FilterIndex = 2;
//this gives the title of the savefiledialog.
saveFileDialog1.Title = "save file";
//this prompts the user if they want to overwrite an existing file.
saveFileDialog1.OverwritePrompt = true;
//gets the input made by the savefiledialog.
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//saves the file.
richTextBox1.SaveFile(saveFileDialog1.FileName,
//saves the text in the richbox
RichTextBoxStreamType.RichText);
I want to be able to save as ether a rtf or a txt format. thanks.
Use filename different name and pass it to SaveFile with the read content buffer from origianl file.