I will spare you for a big background story to the program I'm creating here.
image
As the picture shows:
How do i get the directory result from the lowest method into the one above as a path string?
One way to do the job is by having a global variable like this:
string dir = ""; //Default
private void SelectDir_Click(object sender, EventArgs e)
{
//Open dialog and in dialog ok set dir
dir=dialog.Path;
}
private void UserValue_Click(object sender, EventArgs e)
{
var path=dir+"\\fileName.txt";
}
I was to lazy to type a code like yours but you'll get it :)
First, declare the variable to store your string.
private string userSelectedPath = "";
Create the FolderBrowserDialog:
xmodialog = new FolderBrowserDialog();
Check the result and retrieve the path selected by the user:
var result = xmodialog.ShowDialog();
if (result == DialogResult.OK)
{
userSelectedPath = xmodialog.SelectedPath;
}
Finally, you can use the stored path however you like:
File.WriteAllText(..., "A6_DRV_EDI=" + userSelectedPath);
It is up to you to enforce that the user first selects the path and only then uses it.
Related
I'm looking for an option that allows a user to input their destination folder (Usually copy/Paste) into a text box on a Windows Application, and append the file's name with a specific name. However, I'm stuck as to how to accomplish this.
//Exporting to CSV.
string folderPath = MessageBox.Show(textBox1_TextChanged);
File.WriteAllText(folderPath + "DIR_" + (DateTime.Now.ToShortDateString()) + ".csv", csv);
So it can look like: C:/DIR_9132017.csv, or Documents/DIR_9132017.csv, depending on what the user inputs into the textbox. I have nothing in my textbox code section at the moment, if that also gives a clearer picture about the situation.
Any help will be greatly appreciated. Thank you!
You can either use FolderBrowserDialog or can just copy/paste the path and create the directory.
Using FolderBrowserDialog
Step1 : Create Browse button (so that the user can choose the directory)
Step2 : Create Export button (Place the code to write the file here)
private void browseButton_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
var folderPath = folderBrowserDialog1.SelectedPath;
textBox1.Text = folderPath;
}
}
private void exportToCsvButton_Click(object sender, EventArgs e)
{
var path = textBox1.Text;
var file = Directory.CreateDirectory(path);
var filename = "DIR_" + (DateTime.Now.ToShortDateString()) + ".csv";
File.WriteAllText(Path.Combine(path, "test.csv"), "content");
}
Using Copy/Paste
Step1 : Create Export button (User copy pastes the path. System create the directory and writes the file)
private void exportToCsvButton_Click(object sender, EventArgs e)
{
var path = textBox1.Text;
var file = Directory.CreateDirectory(path);
var filename = "DIR_" + (DateTime.Now.ToShortDateString()) + ".csv";
File.WriteAllText(Path.Combine(path, "test.csv"), "content");
}
You would use a FolderBrowserDialog for that. After adding it to your form, you can use it like this:
public void ChooseFolder()
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
folderPath = folderBrowserDialog1.SelectedPath;
}
}
Source: https://msdn.microsoft.com/en-us/library/aa984305(v=vs.71).aspx
You haven't specified whether it's WinForms or WPF, so I'm using WPF, but is should be the same in WinForms.
To select the folder, you could use FolderBrowseDialog as follows. This code should be placed inside a button or some other similar control's Click event.
if (folderBrowse.ShowDialog() == DialogResult.OK)
{
txtPath.Text = folderBrowse.SelectedPath;
}
txtPath being a TextBox your selected path displayed in. You can substitute it with a simple string variable if you don't want to use a TextBox.
And if you want the user to be able to drag-drop a folder into a TextBox, you can create a TextBox control, and in it's PreviewDragOver and Drop events, you can do the following.
private void txtPath_PreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}
private void txtPath_Drop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null && files.Length != 0)
txtPath.Text = files[0];
}
Using both above in combination user has the ability to either select the folder he/she wants by clicking a button, drag-and-drop a folder into the TextBox, or copy-paste the folder path into the TextBox.
So basically what I'm trying to accomplish is being able to select a file from a displayed list and open that file. Right now I have it set up in a CheckBoxList that displays the .docx, .mov, and .txt files that exist in the selected folder. The problem is I can't get it to open the file. I've seen most people suggesting-
Process.Start(filename);
But the problem with that is that it requires a specific file name and I'm trying to pull that name from a variable. Any ideas?
Here's my current code -
private void Form1_Load(object sender, EventArgs e)
{
const string path = #"C:\Users\Haxelle\Documents\Journal";
List<string> extensions = new List<string> { "DOCX", "MOV", "TXT" };
string[] files = GetFilesWithExtensions(path, extensions);
ckbEntry.Items.AddRange(files);
}
private string[] GetFilesWithExtensions(string path, List<string> extensions)
{
string[] allFilesInFolder = Directory.GetFiles(path);
return allFilesInFolder.Where(f => extensions.Contains(f.ToUpper().Split('.').Last())).ToArray();
}
private void btnOpen_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = ckbEntry.CheckedItems;
}
Trying to open file in btnOpen_Click
It seems like all you are missing is iterating over the selected files names and opening them. Since the CheckedItemCollection.Item is typed as object, you will need to cast the items, which can be done using LINQ's Cast function.
private void btnOpen_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = ckbEntry.CheckedItems;
foreach (var filename in selectedFiles.Cast<string>()) {
Process.Start(filename);
}
}
Hi I am trying to save only the path of a file using fileupload . On clicking the button I want only the complete path that the user has selected into the file upload to be stored into the database. Just for testing purposes I am using labels in the code below but ultimately I will connect it to a data base.I only need to store the path selected by the user and not the file.
HTML
C# that I have been trying but not working
protected void Button1_Click(object sender, EventArgs e)
{
string g = FileUpload1.FileName;
string b =Convert.ToString(FileUpload1.PostedFile.InputStream);
//string filepath = Path.GetFullPath(FileUpload1.FileName.toString());
Label1.Text = g;
Label2.Text =b;
}
change your code as below:
protected void Button1_Click(object sender, EventArgs e)
{
string g = Server.MapPath(FileUpload1.FileName);
string b =Convert.ToString(FileUpload1.PostedFile.InputStream);
//string filepath = Path.GetFullPath(FileUpload1.FileName.toString());
Label1.Text = g;
Label2.Text =b;
}
You can't.
See also here: How can I get file.path in plupload?
If I get you correctly, this is some kind of intranet application?
If this needs to work within a closed domain, you might as well think about a desktop application instead.
You need a path for save file? Try this:
this.MapPath("~")
or
this.yourUploadFile.PostedFile.SaveAs(this.MapPath("~") + "YOUR FOLDER + NAME");
I have a textbox that generates a code based on the selections made by the user. I would like each possible code to correlate with copying and moving a folder into another. The folder is chosen by another textbox that allows the user to manually select the path for the new files to be moved to. What I am looking to do is set up a string of if/else if statements for each of the possible codes from textbox1. Take a look at my code below and see wha tyou find. Everything seems to work except for my statements uder
Private void button1_click...
private void button1_Click(object sender, EventArgs e)
{
string destination = textBox1.Text;
if (textBox2.Text == "111")
String sourceFile = (#"C:\Program Files (x86)\OrganizerPro\TFSN Internal Advisor to SWAG PPW");
System.IO.File.Move(sourceFile, destination);
}
Your immediate problem might just be one of scoping, it looks like you try the move even if the if failed to set the value.
A list of If/else is not a very maintainable solution, you'll need to rebuild and redeploy each time the list of possibilities changes. Avoid this by reading the list from something that is external to the application.
However, what you describe is essentially a mapping between a code and a filename.
If your mapping really is static and you're happy for it to be baked into the application then you could define the mapping like this
private Dictionary<string, string> mapping = new Dictionary<string, string>
{
{ "111", #"c:\Program Files\File 1.txt" },
{ "112", #"c:\Program Files\File 2.txt" },
{ "113", #"c:\Program Files\File 3.txt" },
};
You can retrieve values using some simple Linq
var codeLookup = mapping.FirstOrDefault(kv => kv.Key == textBox2.Text);
if (codeLookup.Key != null)
{
System.IO.File.Move(codeLookup.Value, destination);
}
Your question is not very clear. But there is something not logic in your code. this is a code behind method so change the method's access modifier from private to protected or public cause the event click does not reach this method
private void button1_Click(object sender, EventArgs e)
to
protected void button1_Click(object sender, EventArgs e)
Essentially I have a button and a text box and when the user inputs text and hits the button i want it to create anew folder in a selected destination, ive got my code currently and cant figure out why it wont work
private void button1_Click(object sender, EventArgs e)
{
if (!Directory.Exists("C:\\Users\\Ben\\Documents\\CreateDirectoryTest" + Searchbox.Text))
{
Directory.CreateDirectory("C:\\Users\\Ben\\Documents\\CreateDirectoryTest" + Searchbox.Text);
}
}
am i missing something? help would be really appreciated
Don't concatenate file system path's manually. Use the methods of System.IO:
private void button1_Click(object sender, EventArgs e)
{
const string path = "C:\\Users\\Ben\\Documents\\CreateDirectoryTest\\";
var directory = System.IO.Path.Combine(path, Searchbox.Text);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
I assume you're trying to check for a subdirectory of CreateDirectoryTest and create a directory inside of it if not. The way you're concatenating the string, if Searchbox.text is "TheFolder" for example, your string would end up looking like this:
C:\Users\Ben\Documents\CreateDirectoryTestTheFolder
You can either add a \\
if (!Directory.Exists("C:\\Users\\Ben\\Documents\\CreateDirectoryTest\\" + Searchbox.Text))
{
Directory.CreateDirectory("C:\\Users\\Ben\\Documents\\CreateDirectoryTest\\" + Searchbox.Text);
}
Or just use Path.Combine:
string path = System.IO.Path.Combine("C:\\Users\\Ben\\Documents\\CreateDirectoryTest", Searchbox.Text);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}