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
366 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

689 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