Solved

Is it possible to save a file without using save file dialog in a particular location?(Visual studio/C#)

Posted on 2008-11-01
3
316 Views
Last Modified: 2013-11-26
I am using save file dialog to save file file into a particular location say "C:\router\"
Is it possible to save a file just by clicking 'save' button to it name the file with the names in one of the text box and saves it in"C:\router\" without human interaction.

PS:
I am using this to create contents of the file
 {StringBuilder sr = new StringBuilder();
sr.AppendLine("interface Ethernet0");
            richTextBox1.Text = sr.ToString();

{StringBuilder sr = new StringBuilder();

sr.AppendLine("interface Ethernet0");

            richTextBox1.Text = sr.ToString();  
 

   private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                SaveFileDialog sfldg = new SaveFileDialog();

                sfldg.Filter = "Configuration File(*.cfg)|*.cfg";

                sfldg.DefaultExt = "*.cfg";
 

                if (sfldg.ShowDialog() == DialogResult.OK)

                {

                    using (StreamWriter sw = new StreamWriter(sfldg.FileName))

                    {

                        sw.Write(sr.ToString());

                        MessageBox.Show("Configuration file was saved.", "Success");

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }       

        }

Open in new window

0
Comment
Question by:JesuSingarayar
3 Comments
 
LVL 12

Accepted Solution

by:
jandromeda earned 125 total points
ID: 22859850
Try this.
private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                string fileName = @"C:\router\File1.txt";

 

                StreamWriter sw = new StreamWriter(fileName );

                    sw.Write(sr.ToString());

                    MessageBox.Show("Configuration file was saved.", "Success");

                    sw.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }       

        }

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22859862
Your question looks confusing. Assuming you want to take just the filename but not the folder name and save to an specific location, you can do something like:

using (StreamWriter sw = new StreamWriter(Path.Combine(@"C:\router\", Path.GetFilename(sfldg.FileName))))
                   
0
 

Author Closing Comment

by:JesuSingarayar
ID: 31512433
Thanks jandromeda!!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# application error "Parameter Is Not Valid". 3 79
Excel file not created as expected 7 50
Replace &lt; with < 14 56
Web Form VB.Net  import CSV 4 27
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now