Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# browse directory

Posted on 2010-11-07
3
Medium Priority
?
559 Views
Last Modified: 2012-05-10
the code below if i want user to input file name in

 string source = @"c:\song\sa.mp3";

by click bottom then it can browse and he can select file name how can i do that in  csharp


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string source = @"c:\song\sa.mp3";
            string dest = @"c:\song1\01sa.mp3";

            string destDir = Path.GetDirectoryName(dest);
            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }

            File.Copy(source, dest, true);

        }
    }
}
0
Comment
Question by:teera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1332 total points
ID: 34081679
I'm not sure what your question is, but here's a simple example using an OpenFileDialog to select the file to be copied, and a FolderBrowserDialog to select the folder to copy it to.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void SourceFileBrowseButton_Click(object sender, EventArgs e)
		{
			using (OpenFileDialog ofd = new OpenFileDialog())
			{
				ofd.Filter = "MP3 Files (*.mp3)|*.mp3|All Files (*.*)|*.*";
				ofd.FilterIndex = 1;
				ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
				ofd.Title = "Select a file to copy...";
				ofd.ValidateNames = true;
				if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
					SourceFileTextBox.Text = ofd.FileName;
			}
		}

		private void DestinationFolderBrowseButton_Click(object sender, EventArgs e)
		{
			using (FolderBrowserDialog fbd = new FolderBrowserDialog())
			{
				fbd.RootFolder = Environment.SpecialFolder.MyComputer;
				fbd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
				if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
					DestinationFolderTextBox.Text = fbd.SelectedPath;
			}
		}

		private void CopyFileButton_Click(object sender, EventArgs e)
		{
			if (!File.Exists(SourceFileTextBox.Text))
			{
				MessageBox.Show("Source File Does Not Exist");
				return;
			}

			if (!Directory.Exists(DestinationFolderTextBox.Text))
			{
				MessageBox.Show("Destination Folder Does Not Exist");
				return;
			}

			string sourceFolder = Path.GetDirectoryName(SourceFileTextBox.Text);
			string sourceFile = Path.GetFileName(SourceFileTextBox.Text);
			string destinationPath = Path.Combine(DestinationFolderTextBox.Text, sourceFile);

			if (File.Exists(destinationPath))
				destinationPath = GetNumberedFileName(DestinationFolderTextBox.Text, sourceFile);

			File.Copy(SourceFileTextBox.Text, destinationPath);

			SourceFileTextBox.Text = String.Empty;
			DestinationFolderTextBox.Text = String.Empty;
		}

		private string GetNumberedFileName(string folder, string file)
		{
			string fileBase = Path.GetFileNameWithoutExtension(file);
			string extension = Path.GetExtension(file);

			for (int i = 2; i < 10000; i++)
			{
				string testFileName = String.Format(@"{0}\{1} ({2}){3}", folder, fileBase, i, extension);
				if (!File.Exists(testFileName))
					return testFileName;
			}
			return String.Empty;
		}
	}
}

Open in new window

0
 
LVL 9

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 668 total points
ID: 34083167
In your form
1. Create a textbox,  let's name: txtMP3
2. Create a button with name:btnSelectMP3, text: Select MP3 or something like this.
3. In the button handler(By Double clicking that button), write the code as described above to select the MP3 file...

Follow the same steps as described above, if needed.
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 1332 total points
ID: 34083619
I thought it was obvious from the code example there are two text boxes, SourceFileTextBox and DestinationFolderTextBox, and three buttons, SourceFileBrowseButton, DestinationFolderBrowseButton and CopyFileButton.  The methods named ButtonName_Click() run when their respective buttons are clicked.
I did forget to mention that I included a method, GetNumberedFileName, which is not directly related to the question - if a file with the same name already exists in the destination this method will return a new file name such as sa (2).MP3, or sa (3).mp3.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question