简体   繁体   中英

C# Save Results from ListBox button?

I created a new project in Windows Forms App (.NET Framework), this is the code I have:

private void SaveProxyResults_Click(object sender, EventArgs e)
{
    SaveFileDialog dlg = new SaveFileDialog();

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        StreamWriter writer = new StreamWriter(dlg.FileName);

        for (int i = 0; i < GatheredProxies.Items.Count; i++)
        {
            writer.WriteLine((string)GatheredProxies.Items[i]);
        }

        writer.Close();
    }

    dlg.Dispose();
}

With this current code the save file menu pops up but it doesn't automatically go to your Desktop and there's no file type chosen automatically, I also can't save it as.txt because it gives me an error.

How do I have to edit the code in order to make it automatically choose.txt as file format, be able to type in the file name and automatically select your Desktop as file saving location while still being able to change the location of where the file is supposed to be saved?

            Stream myStream;
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            dlg.FilterIndex = 2;
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = dlg.OpenFile()) != null)
                {
                    StreamWriter writer = new StreamWriter(dlg.FileName);

                    for (int i = 0; i < GatheredProxies.Items.Count; i++)
                    {
                        writer.WriteLine((string)GatheredProxies.Items[i]);
                    }

                    writer.Close();
                }

                dlg.Dispose();
            }

Gives me the error:

System.IO.IOException: 'The process cannot access the file 'C:\Users\JP\Desktop\Scraped Proxies123' because it is being used by another process.'

According to your description, you want it to automatically select.txt as the file format

and be able to type the file name and automatically select the desktop as the file saving

location.

You can try the following code to solve this problem.

            Stream myStream;
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "";          
            dlg.InitialDirectory = @"C:\Users\username\Desktop";// Use the absolute path of your computer desktop
            dlg.Filter = "txt files (*.txt)|*.txt";
            dlg.FilterIndex = 1;
            if (dlg.ShowDialog() == DialogResult.OK)
            {       
                if ((myStream = dlg.OpenFile()) != null)
                {
                    myStream.Close();
                    StreamWriter writer = new StreamWriter(dlg.FileName);

                    for (int i = 0; i < GatheredProxies.Items.Count; i++)
                    {
                        writer.WriteLine((string)GatheredProxies.Items[i]);
                    }

                    writer.Close();
                }

            }

            dlg.Dispose();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM