Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Could someone tell me how to retrieve the filename and it's path after a user selected a file on his harddrive please?

What I have tried:

I have nothing for the moment. I'm using an OpenFileDialog object and a SaveFileDialog object. I've read on MSDN about directory.GetFiles but it's not applicable for me. As a matter of fact, all information about GetFiles are for paths with a specific string
(
string[] dirs = Directory.GetFiles(@"c:\", "c*");

or to split the path, or to move folders from 1 directory to another, etc...
And in my case there is no string to give in except for the chosen path of the user.
EDIT
private void btnSavechanges_Click(object sender, EventArgs e)
        {
            //Text from the richtextbox
            string strsave = richTextBox1.ToString();
            //Create a new SaveFileDialog object
            using (SaveFileDialog sfdSave = new SaveFileDialog())
                try
                {
                    //Available file extension
                    sfdSave.Filter = "XML (*.xml*)|*.xml*";
                    //Show SaveFileDialog
                    if (sfdSave.ShowDialog() == DialogResult.OK && sfdSave.FileName.Length > 0)
                    {
                        ////use the original filename and add " -NEG" behind
                        //FileStream fs = (FileStream)saveFileDialog1.OpenFile();
                        //save file as xml
                        UTF8Encoding utf8 = new UTF8Encoding();
                        StreamWriter sw = new System.IO.StreamWriter(sfdSave.FileName, false, utf8);
                        sw.Write(strsave);
                        sw.Close();
                    }
                }
                catch (Exception errorMsg)
                {
                    MessageBox.Show(errorMsg.Message);
                }

            Application.Exit();
        }
Posted
Updated 1-Mar-17 3:34am
v4
Comments
[no name] 1-Mar-17 8:36am    
If you want the filename then why are you messing around with Directory? How are you allow your user to select a file? That is what you should be looking at.
AAB40 1-Mar-17 9:02am    
it's not my directory! is a copied example of the mandatory string in the example of the one who posted this.

MY SOLUTION:
I've added a label on top of the RTB to get out the pathname. Once loaded in the label I could re-use it for the saving as thus:
        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            // Create an OpenFileDialog object.
            OpenFileDialog openFile1 = new OpenFileDialog();

            // Initialize the filter to look for text files.
            openFile1.Filter = "Xml Files|*.xml";
            openFile1.InitialDirectory = @"C:\";
            // If the user selected a file, load its contents into the RichTextBox. 
            if (openFile1.ShowDialog() == DialogResult.OK)
            {
                var FileChosenName = Path.GetFileNameWithoutExtension(openFile1.Fi‌​leName);
                label1.Text = FileChosenName;
                richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
            }

        }

private void btnSavechanges_Click(object sender, EventArgs e)
        {
            //Text from the richtextbox
            string strsave = richTextBox1.ToString();
            string ReadNameOfFile = label1.Text;
            //Create a new SaveFileDialog object
            using (SaveFileDialog sfdSave = new SaveFileDialog())
                try
                {
                    //Available file extension
                    sfdSave.Filter = "XML (*.xml*)|*.xml*";
                    sfdSave.FileName = label1.Text + " -NEG";
                    //Show SaveFileDialog
                    if (sfdSave.ShowDialog() == DialogResult.OK && sfdSave.FileName.Length > 0)
                    {
                        //FileStream fs = (FileStream)saveFileDialog1.OpenFile();
                        //save file as xml
                        UTF8Encoding utf8 = new UTF8Encoding();
                        StreamWriter sw = new System.IO.StreamWriter(sfdSave.FileName, false, utf8);
                        sw.Write(strsave);
                        sw.Close();
                    }
                }
                catch (Exception errorMsg)
                {
                    MessageBox.Show(errorMsg.Message);
                }

            Application.Exit();
        }
 
Share this answer
 
v2
Comments
Graeme_Grant 1-Mar-17 9:37am    
Well done in working it out. :)
OpenFileDialog and SaveFileDialog will give you what you need: How to: Open Files Using the OpenFileDialog Component[^]
 
Share this answer
 
Comments
AAB40 1-Mar-17 8:48am    
Ok, Apparently I'm very bad in expressing myself. Trying again:
User clicks on button "GetFile". Behind the button I have the code for the OpenFileDialog. The filedialog opens the directory where the user can choose a file. The content of the file will open in a RTB.
After changing anything in it, the user wants to save with the same original name + " NEG" behind it, prefilled in the SaveFileDialog. how can I do this please?
Graeme_Grant 1-Mar-17 8:52am    
AAB40 1-Mar-17 9:00am    
No idea how I should do this in my current code (edited post)
Graeme_Grant 1-Mar-17 9:08am    
The code is in the link provided.
Graeme_Grant 1-Mar-17 9:25am    
If you are still struggling, here is the Google Search used to locate the Microsoft link provided: winform pass filename to savedialog - Google[^] - there are a tonne more examples for you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900