?
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
Medium Priority
?
394 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
[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
3 Comments
 
LVL 12

Accepted Solution

by:
jandromeda earned 500 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

764 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