private void btn_add_image_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Choose a file";
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = " JPEG Files (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg|All Files (*.*)|*.*";
openFileDialog1.ShowDialog();
string file_name = openFileDialog1.FileName;
string filename2 = openFileDialog1.SafeFileName;
pictureBox1.Image = Image.FromFile(file_name);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string[] extension = getExtension("images\\" + userid);
if (File.Exists("images\\" + userid + extension[0]))
{
File.Delete("resimler\\" + userid + extension[0]);
}
}
catch (Exception)
{
MessageBox.Show("İmage cannot find");
}
I want to Change File name and save , so i wrote this code if file exists , than delete file and save the choosen with userid name but i cant do change name and save file
if (File.Exists(#"\path\to\source"))
{
File.Move(#"\path\to\source",#"\path\to\destination")
}
I think both your problems can me handled with this bit of code.
System.IO.File.Move("old_file_name_path", "new_file_name_path");
This moves the file to a new filename. Take a look here: File.Move
But, I really don't get what you are asking here:
i wrote this code if file exists , than delete file and save the
choosen with userid name but i cant do change name and save file
Can you be more specific?
Thanks All
private void btn_save_Click(object sender, EventArgs e)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string source = openFileDialog1.FileName;
string[] extension = getExtension(source);
string destination = "images\\" + userid + extension[0];
System.IO.File.Move(source, destination);
pictureBox1.Image = Image.FromFile("images\\" + userid + extension[0]);
}
Related
I tried to use the method
System.IO.File.Move(sourceFile,newFilePath);
to rename a File, but I always get the same error message, that my file couldn't be found.
I tried to manually write the source Path
System.IO.File.Move(#"D:\Users\XXX\Desktop\TestOrHMoin",#"D:\Users\XXX\Desktop\TestOrHMoinNEW");
but I still get the same ERROR Message.
This is my full code
`
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog = new OpenFileDialog();
if (openfiledialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
oldFilePath = openfiledialog.FileName;
listBox1.Items.Add(oldFilePath);
}
}
private void button2_Click(object sender, EventArgs e)
{
newFilePath = textBox1.Text;
oldFilePath = oldFilePath.Remove(oldFilePath.Length-newFilePath.Length);
newFilePath = oldFilePath + newFilePath;
string sourceFile = #oldFilePath;
string newFile = #newFilePath;
MessageBox.Show(sourceFile);
System.IO.File.Move(sourceFile,newFilePath);
// This part is the real code, the above Part is for debugging/testing
//System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
//if (fi.Exists)
//{
// fi.MoveTo(newFilePath);
// MessageBox.Show("Erfolgreich geändert");
//} else { MessageBox.Show("Abbruch"); }
}
`
oldFilePath = oldFilePath.Remove(oldFilePath.Length-newFilePath.Length);
was wrong
I needed to declare another variable, so oldFilePath gets untouched. It was my error. I still don't know, why the manuell direction got an error.
I am creating an app that i need to copy some png files.
I already have searching with many keywords, finding many solutions and none of them worked, so i decided to ask here.
There is the code,it uses a windows forms and "this" refers to this window
private void button2_Click(object sender, EventArgs e)
{
//yes i commented them to solve why the file was not copying
//try
{
FileInfo x = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\some_location");
x.CopyTo(textBox1.Text);
}
//catch
{ }
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select Image To Load";
openDialog.Filter = "Text Files (*.png)|*.png" + "|" + "All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
string PathData = openDialog.FileName;
textBox1.Text = PathData;
}
}
I have gotten several different errors, but most common there is:
System.UnauthorizedAccessException
It looks a bit weird to me that you're using an OpenFileDialog to select a destination. I'd assume you'd want to either do it the other way around:
private void button2_Click(object sender, EventArgs e)
{
//yes i commented them to solve why the file was not copying
//try
{
FileInfo x = new FileInfo(textBox1.Text);
x.CopyTo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\some_location");
}
//catch
{ }
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select Image To Load";
openDialog.Filter = "Text Files (*.png)|*.png" + "|" + "All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
string PathData = openDialog.FileName;
textBox1.Text = PathData;
}
}
or use SaveFileDialog instead.
I am trying to output the file address of an item selected in a combobox. But i keep getting the Directory address of the project and not the item itself. Please help. Here is my Code:
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (availableSoftDropBox.SelectedItem.Equals("Choose Your Own..."))
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
txtFlashFile.Text = openFileDialog1.FileName;
}
else
{
string fileName;
fileName = Path.GetFullPath((string)availableSoftDropBox.SelectedItem);
string fullPath = #"C:\Program Files (x86)\yeet-n . master\yeet-
master\src\yeet\System\Products\" + (fileName);
txtFlashFile.Text = fullPath;
I'm not quite sure what you want to achieve, but using FileInfo instead of Path.GetFullPath might help.
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
const string fullPath = #"C:\Program Files (x86)\yeet-n . master\yeet-
master\src\yeet\System\Products\";
string selection = (string)availableSoftDropBox.SelectedItem;
var fileInfo = new FileInfo(fullPath + selection);
string text = fileInfo.FullName;
if (selection.Equals("Choose Your Own..."))
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
text = openFileDialog1.FileName;
}
}
txtFlashFile.Text = text;
}
Currently, I have a browser dialog that opens and allows the user to select a folder in which doc / docx files will be merged into one file. At the moment, it is rigged up to merge files once the 'DialogResult.ok' button is dismissed in the browser dialog. as shown below:
private void browseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog diagBrowser = new FolderBrowserDialog();
diagBrowser.Description = "Select a folder which contains files needing combined...";
// Default folder, altered when the user selects folder of choice
string selectedFolder = #"C:\";
diagBrowser.SelectedPath = selectedFolder;
// initial file path display
folderPath.Text = diagBrowser.SelectedPath;
if (DialogResult.OK == diagBrowser.ShowDialog())
{
// Grab the folder that was chosen
selectedFolder = diagBrowser.SelectedPath;
folderPath.Text = diagBrowser.SelectedPath;
}
private void combineButton_Click(object sender, EventArgs e)
{
string[] AllDocFolder = Directory.GetFiles(selectedFolder, "*.doc");
string outputFileName = (#"C:\Test\Merge\Combined.docx");
MsWord.Merge(AllDocFolder, outputFileName, true);
// Message displaying how many files are combined.
MessageBox.Show("A total of " + AllDocFolder.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
the issue i am having is that i want the 'combineButton' to merge the documents as opposed to the 'DialogResult.ok'. When i copy the lines:
string[] AllDocFolder = Directory.GetFiles(selectedFolder, "*.doc");
string outputFileName = (#"C:\Test\Merge\Combined.docx");
MsWord.Merge(AllDocFolder, outputFileName, true);
into the combineButton area, i get an error saying 'the name 'selectedFolder' does not exist in the current context'. This may be a stupid question, but is there a quick way to remedy this?
As far as I understand your problem, you want to split the folder selection and the merging of the documents, right?
So you could put the information about the target directory into a class variable:
public class MyForm
{
private string[] _sourceFiles;
private void browseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog diagBrowser = new FolderBrowserDialog();
diagBrowser.Description = "Select a folder which contains files needing combined...";
// Default folder, altered when the user selects folder of choice
string selectedFolder = #"C:\";
diagBrowser.SelectedPath = selectedFolder;
// initial file path display
folderPath.Text = diagBrowser.SelectedPath;
if (DialogResult.OK == diagBrowser.ShowDialog())
{
// Grab the folder that was chosen
selectedFolder = diagBrowser.SelectedPath;
folderPath.Text = diagBrowser.SelectedPath;
_sourceFiles = Directory.GetFiles(selectedFolder, "*.doc");
}
}
private void combineButton_Click(object sender, EventArgs e)
{
if (_sourceFiles != null && _sourceFiles.Length > 0)
{
string outputFileName = (#"C:\Test\Merge\Combined.docx");
MsWord.Merge(_sourceFiles, outputFileName, true);
// Message displaying how many files are combined.
MessageBox.Show("A total of " + _sourceFiles.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Hey Everyone,
Sorry to bother you with this, but I'm having an issue with selecting multiple xlsx files through a file browser window in a winforms application when debugging and can't figure out what I did wrong.
Problem: I set Multiselect=true under the OpenFileDialog but I still cannot select more than one file.
What do I need to change to get the multiSelect feature to work?
Do I need to add anything under sourceFileOpenFileDialog method?
Do I need to add anything under listBoxSourceFiles_SelectedIndexChanged method to get the filenames to load correclty in the listbox?
// When the user clicks on Select Files Button, this happens
private void sourceFiles_Click(object sender, EventArgs e)
{
Stream myStream;
int i = 0;
OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();
this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|" + "All Files (*.*)|*.*";
this.sourceFileOpenFileDialog.FilterIndex = 2;
this.sourceFileOpenFileDialog.RestoreDirectory = true;
this.sourceFileOpenFileDialog.Multiselect = true;
this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
{
using (myStream)
{
foreach (string FileName in sourceFileOpenFileDialog.FileNames)
{
sourceFileOpenFileDialog.FileNames[i] = FileName;
listBoxSourceFiles.Items.Add(FileName);
Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
i++;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
//method for the listbox. Do I need anything here?
private void listBoxSourceFiles_SelectedIndexChanged(object sender, EventArgs e)
{
}
//method for the sourceFileOpenFileDialog. Do I need anything here?
private void sourceFileOpenFileDialog_FileOk(object sender, CancelEventArgs e)
{
}
I updated the code to reflect sourceFileOpenFileDialog and the MultiSelect or Title doesn't work... Perhaps I'm referencing the onfiledialog wrong? is this the proper prefix to use?
Thanks for looking!
You are using two OpenFileDialogs. You display sourceFilesList but you initialized sourceFileOpenFileDialog. Using consistent naming rules religiously is a great way to avoid bugs like these btw.
Next problem, what is OpenFile() supposed to do when you selected more than one file? What is myStream actually used for?
You are setting up sourceFileOpenFileDialog but then use sourceFileList!!! Make up your mind and only use one.
Fixed the non-working MultiSelect by:
Updating the code to only use one variable sourceFileOpenFileDialog throughout the method and the MultiSelect or Title didnt work...
Removing all references to myStream. myStream was used in an example which i based my code off but i took it out and the multiSelect works!
Here's the working code:
// When the user clicks on Select Files Button, this happens
private void sourceFiles_Click(object sender, EventArgs e)
{
Stream myStream;
int i = 0;
OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();
this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|" + "All Files (*.*)|*.*";
this.sourceFileOpenFileDialog.FilterIndex = 2;
this.sourceFileOpenFileDialog.RestoreDirectory = true;
this.sourceFileOpenFileDialog.Multiselect = true;
this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
string tempFolder = System.IO.Path.GetTempPath();
foreach (string FileName in this.sourceFileOpenFileDialog.FileNames)
{
this.sourceFileOpenFileDialog.FileNames[i] = FileName;
listBoxSourceFiles.Items.Add(FileName);
Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
i++;
System.IO.File.Copy(FileName, tempFolder + #"\" + FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
//method for the listbox. Do I need anything here?
private void listBoxSourceFiles_SelectedIndexChanged(object sender, EventArgs e)
{
}
//method for the sourceFileOpenFileDialog. Do I need anything here?
private void sourceFileOpenFileDialog_FileOk(object sender, CancelEventArgs e)
{
}