I have 2 buttons (Button1 - Browse ; Button2 - Upload) and 1 textBox
Here is the scenario. When I click on Browse, it will open a window and will browse for a specific item.
The selected item will display on textbox.
private void Browse_Click(object sender, EventArgs e)
{
btnSample.Title = "Sample File Upload";
btnSample.InitialDirectory = #"c:\";
btnSample.Filter = "TIF|*.tif|JPG|*.jpg|GIF|*.gif|PNG|*.png|BMP|*.bmp|PDF|*.pdf|XLS|*.xls|DOC|*.doc|XLSX|*.xlsx|DOCX|*.docx";
btnSample.FilterIndex = 2;
btnSample.RestoreDirectory = true;
if (btnSample.ShowDialog() == DialogResult.OK)
{
textBox1.Text = btnSample.FileName;
}
}
When I click on the Upload Button the file on the textbox will be on the created folder.
I'm done with creating the folder. but my problem is how can i insert the selected file on the folder.
private void button4_Click(object sender, EventArgs e)
{
string path = #"C:\SampleFolder\NewFolder";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
MessageBox.Show("Successfully Created New Directory");
}
else
{
MessageBox.Show("Filename Exist");
}
}
var sourceFilePath = #"C:\temp\file.txt";
var destFilePath= #"C:\otherFolder\file.txt";
If you want to move the file:
File.Move(sourceFilePath, destFilePath);
If you want to copy the file
File.Copy(sourceFilePath, destFilePath);
easy huh?
ofcourse, you'd have to adapt the paths to your problem...
Related
Maybe this is an old question, but I canĀ“t find a solution fpr my project. Let me explain:
I have 3 Textboxes, who I can fill with values.
1 Button who save the values of the textboxes in a *.txt file. The Name of the file is the value of the first textboxes.
Then there is a ComboBox where I can select the txt files.
But I cant find the right code, when I selected a file, I want to see the 3 values(3 different lines) of the file into the Textboxes. Can someone help me, I will show you the Codes maybe its easier to understand.
the first code to save the values (tb1-3 = Textbox) in the txt file:
private void savebutton_click(object sender, RoutedEventArgs e)
{
string folder = #"C:\Users\...\...\...\Debug\";
string filename = tb1.Text + ", " + tb2.Text;
string writerfile = folder + filename;
using (StreamWriter writer = new StreamWriter(writerfile))
{
writer.WriteLine(this.tb1.Text);
writer.WriteLine(this.tb2.Text);
writer.WriteLine(this.tb3.Text);
}
}
The second code is to show the txt files in the combobox(combo1):
private void comboboxloaded(object sender, RoutedEventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\...\...\...\Debug");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
combo1.Items.Add(file);
}
}
And know... I need help...
private void comboboxvalueselect(object sender, SelectionChangedEventArgs e)
{
// Maybe?????
string fileName = combo1.SelectedItem.ToString();
string filePath = System.IO.Path.Combine(#"C:\Users\...\...\...\Debug" + fileName + "*.txt");
// and something with StreamReader??????
}
If I understand the question it could be as simple as using File.ReadAllLines
Opens a text file, reads all lines of the file into a string array,
and then closes the file.
private void comboboxvalueselect(object sender, SelectionChangedEventArgs e)
{
const string somePath = #"C:\Users\...\...\...\Debug";
var fileName = combo1.SelectedItem.ToString();
var filePath = Path.Combine(somePath , fileName);
// where you need those lines to go
someTextBox.Text = File.ReadAllLines(filePath);
}
I have a program that enables a user to search text files in an open file dialog. The user is then able to open an existing text file they choose and edit it. However, my problem is that when they file opens it appears blank. What am I missing?
private void Open_Click(object sender, RoutedEventArgs e)
{
TextBox openText = new TextBox();
var OpenFile = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> Success = OpenFile.ShowDialog();
OpenFile.DefaultExt = ".txt";
OpenFile.Filter = "Text documents (.txt)|*.txt";
if (Success.HasValue && Success.Value)
{
openText.Text = OpenFile.FileName;
}
else
{
//cannot open file
}
}
Replace this:
openText.Text = OpenFile.FileName;
with this:
openText.Text = System.IO.File.ReadAllText(OpenFile.FileName);
Use File.ReadAllText()
openText.Text = File.ReadAllText(OpenFile.FileName);
I'm creating a FileSystemWatcher in windows application using c# that will send the latest file to HTTP server.
1) In this FileSystemWatcher, after clicking on the browse button the user has the choice to select the folder/directory(where the latest files will be downloaded).
2) After clicking on the Monitor button, it'll monitor for new file that are downloaded from internet or mailbox. When user downloads any new file, it'll trigger an event(i.e., message box specifying the FileName with two buttons i.e., YES and NO).
3)Along with this,it'll also be showing a complete filepath of the latest file in the file system watcher.
4) The latest file will come in the directory(here directory will be according to the user's choice) and will send those files along with data to http server.
My question is that how can i send those files(i.e., latest files that are downloaded from internet) along with some data (after clicking on YES button from the message box) to http server without specifying the file path.
I'm able to send files by specifying the complete filepath from my local. But in this case, the filepath may be different according to user's choice.After clicking on YES button,how can i send the latest file(i.e., downloaded from internet or mailbox) along with some token or data to HTTP server in c#. I'm not able to to understand that how it'll pick the file that is shown in the messagebox.
This is my code:-
//This is FileSystemWatcher Created Event
private void fileWatcher_Created(object sender,System.IO. FileSystemEventArgs e)
{
textBox3.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
textBox3.Focus();
textBox3.Select(textBox3.TextLength, 0);
textBox3.ScrollToCaret();
}
//This is FileSystemWatcher Renamed_1Created Event
private void fileWatcher_Renamed_1(object sender, RenamedEventArgs e)
{
// System.Threading.Thread.Sleep(5000);
textBox3.Text += e.ChangeType + ": " + e.FullPath+ "\r\n";
textBox3.Focus();
textBox3.Select(textBox3.TextLength, 0);
textBox3.ScrollToCaret();
try
{
System.Threading.Thread.Sleep(3000);
fileWatcher.EnableRaisingEvents = false;
DialogResult result = MessageBox.Show(new Form { TopMost = true }, "fileName:-" + e.Name, "New File", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
WebClient client = new WebClient();
string myFile = #"FilePath";
client.UploadFile("url", "POST", myFile);
MessageBox.Show("Sent");
client.Dispose();
//If user click NO button
if (result == DialogResult.No)
{
Close();
}
}
}
finally
{
fileWatcher.EnableRaisingEvents = true;
}
}
//This is textBox of FileSystemWatcher where the filename and file path will be shown
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.ScrollBars = ScrollBars.Vertical;
}
//This is monitor button
private void button1_Click(object sender, EventArgs e)
{
fileWatcher.Path = #textBox1.Text;
fileWatcher.Filter = textBox2.Text;
fileWatcher.IncludeSubdirectories = checkBox1.Checked;
// fileWatcher.EnableRaisingEvents = true;
button1.BackColor = Color.DarkGray;
}
//This is the stop button
private void button2_Click(object sender, EventArgs e)
{
// fileWatcher.EnableRaisingEvents = false;
button2.BackColor = Color.DarkGray;
}
//This is Browse button
private void button3_Click(object sender, EventArgs e)
{
button3.BackColor = Color.DarkGray;
// Create a new instance of FolderBrowserDialog.
FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
// A new folder button will display in FolderBrowserDialog.
folderBrowserDlg.ShowNewFolderButton = true;
//Show FolderBrowserDialog
DialogResult dlgResult = folderBrowserDlg.ShowDialog();
if (dlgResult.Equals(DialogResult.OK))
{
//Show selected folder path in textbox1.
textBox1.Text = folderBrowserDlg.SelectedPath;
//Browsing start from root folder.
Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
}
}
Please help me through this.
Thanks
I want to get the database name from Database Selection when I select the path in Database Backup... For E.g: I selected the name of database name 'Sample' and then I click Browse button and choose the Path where I want to backup... Now, I want the selected database name Sample after the Location...
For Instance: C:\Users\Abc\Desktop\Sample... Help to me to get the selected name of database in a text box.
Code for Browse button of Backup:
private void btnBrowseBa_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if(dlg.ShowDialog()== DialogResult.OK)
{
txtLocBa.Text = dlg.SelectedPath;
}
}
FolderBrowserDialog dlg = new FolderBrowserDialog();
if(dlg.ShowDialog()== DialogResult.OK)
{
var database = yourDatabaseComboBox.SelectedItem.ToString();
var extension= "bak";
var databaseFileName = string.Format("{0}.{1}", database, extension);
txtLocBa.Text = System.IO.Path.Combine(dlg.SelectedPath, databaseFileName);
}
Well, depending on what your combobox's name is:
private void btnBrowseBa_Click(object sender, EventArgs e)
{
string path = "";
FolderBrowserDialog dlg = new FolderBrowserDialog();
if(dlg.ShowDialog()== DialogResult.OK)
{
path = Path.Combine(dlg.SelectedPath, comboBox.Text);
}
}
Just concatenate:
txtLocBa.Text = dlg.SelectedPath + #"\" + comboBox1.Text
Try this which will get the path:
path = Path.Combine(backupPath, databaseNameComboBox.Text);
// Browses file with OpenFileDialog control
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
}
}
In the code above, i browse for a file to open. What I want to do is, browse for a file, select it and then press ok. On clicking ok, i want to make a copy of the seleted file and give that duplicate file a .txt extension. I need help on achieving this.
Thanks
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
var fileName = openFileDialogCSV.FileName;
System.IO.File.Copy( fileName ,Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName)+".txt"));
}
Above code will copy selected file as txt with same name and in to same directory.
if you need to overwrite existing file with same name add another parameter to Copy method as true.
System.IO.File.Copy(source, destination, true);
You use File.Copy as follows,
File.Copy(openFileDialogCSV.FileName., openFileDialogCSV.FileName + ".txt");
Try this
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
System.IO.File.Copy(this.txtFileToImport.Text,"C://123.txt")
}
}
123 can be changed by any file name that you want.