-1

I'm been wondering is there a way to save a file in specific's folder in C#?

The only method that I know is 'SaveFileDialog' but the problems is I want to save files

in folder without showing saveFilesDialog's Box.

saveFilesDialog's Box : is a box that prompts you to Click 'YES' or 'CANCEL'.

Code samples

-In form1

public Form1()
 {
   InitializeComponent();
 }

private string Path =@"D:\Files";  //locaction i wanna stores all the files in
private int i = 0;
private button1_Click(object sender,EventArgs e) 
{
    i++;
    SaveDialogFile save = new SaveDialogFile();
    if(Save.

    if (save.ShowDialog() != DialogResult.OK)return; //Prompt's Dialog will show


    save.Filter = "File Text(*.txt)|*.txt";
    save.InitialDirectory = Path;
    save.FileName = "txt"+i.ToString();

    //Goal : i want 'save.FileName' store in 'Path' without Click 'OK' or Show Prompt Dialog's box

    
}

Expect Result [1]: https://i.stack.imgur.com/9JqWO.png

Anyone can help me? I kinda stuck rn :)

This is my full code it's hard to read but you'll get the point

public partial class convertMp3ToWav : Form
    {
        public convertMp3ToWav()
        {
            InitializeComponent();
        }
        BackgroundWorker bw;
        string withoutEx;
        List<string> song_lists = new List<string>();

        private void button1_Click(object sender, EventArgs e)
        {
            bw = new BackgroundWorker();
            bw.DoWork += (obj, ae) => newThread();
            bw.RunWorkerAsync();
        }
        void newThread()
        {

            Thread th = new Thread
            ((ThreadStart)(() =>
            {
                file();
            }));
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
            th.Join();
        }
        void file()
        {
            
            string path = @"D:\musics\wav";
            Directory.CreateDirectory(path);
            FolderBrowserDialog f = new FolderBrowserDialog();
            f.ShowDialog();
            string[] lists = Directory.GetFiles(f.SelectedPath, "*.*", SearchOption.AllDirectories);
            foreach (string list in lists)
            {
                if (Path.GetExtension(list) == ".mp3")
                {
                    string fn = Path.GetFullPath(list);
                    withoutEx = Path.GetFileNameWithoutExtension(fn);
                    song_lists.Add(fn);
                    Console.WriteLine(withoutEx);

                    SaveFileDialog save = new SaveFileDialog();
                    save.Filter = "Wav FIle (*.wav)|*.wav;";
                    //save.FileName = song_lists[0];
                    save.FileName = withoutEx;
                    save.InitialDirectory = path;


                    if (save.ShowDialog() == DialogResult.OK)
                    {
                        using (Mp3FileReader mp3 = new Mp3FileReader(fn))
                        {
                            using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
                            {
                                WaveFileWriter.CreateWaveFile(save.FileName, pcm);
                            }

                        }
                    }
                    
                }

            }
           
            
        }

     
    }

this code's work pretty well!! but i need to click 'OK' everytime!! so is there anyway to save file without click 'OK' everytime!!

Pazzles
  • 9
  • 3
  • How are you writing to a file in the first place? – Dai Sep 04 '21 at 03:24
  • hi@Dai sorry, it's kinda complicated to explain but can you give me code to solve this problems!! – Pazzles Sep 04 '21 at 03:27
  • 4
    A `SaveFileDialog` does not actually “save” the file. It simply gets the path and file name, then YOUR code "saves" the file to that path and file name. You can set the path and file name to anything you want so long as it exists. So, technically, the `SaveFileDialog` and the “actual” saving of the file are two different things. How are you currently saving the files? – JohnG Sep 04 '21 at 03:27
  • @JohnG can you check my code ? – Pazzles Sep 04 '21 at 03:37
  • You need to clarify some things. First, IF you do NOT want to use a `SaveFileDialog` to allow the user to select the path and file name… THEN… YOUR CODE will have to figure out what path and file name to use. Figuring out the directory may be straight forward if the code uses the same folder for all the files, however, figuring out the file names may NOT be as straight forward. What strategy are you planning to use? – JohnG Sep 04 '21 at 04:12
  • It appears the code is saving some music files and you “could” save the file names… something like wave1, wave2 etc.., but I think a file name that describes the group or title of the song would be more appropriate. How is YOUR CODE going to figure this out if the user does not do this using a save file dialog? In other words… What is your plan/strategy to name the “files” if you do not let the user pick the file name? How is the wav file originally “created”? Is the user “creating” these NEW wav files? – JohnG Sep 04 '21 at 04:13
  • 1
    Not wishing to appear rude, but if you're asking how to save a file without using a SaveFileDialog you might not want to be playing with the multithreaded fire – Caius Jard Sep 04 '21 at 06:46
  • @CaiusJard all good !! thanks btw – Pazzles Sep 04 '21 at 13:35

1 Answers1

-1

Conceptually the only thing SaveFileDialog is doing, if you merely click OK when it shows, is changing the file extension*. Use Path.ChangeExtension instead

var pathToSaveWavTo = Path.ChangeExtension(pathToMp3, "wav");

The wav will be saved alongside the mp3. If you want to save it elsewhere, use Path.Combine to build a new path e.g.

pathToSaveWavTo = Path.Combine(@"c:\temp", Path.GetFileName(pathToSaveWavTo));

*I say this because giving it a filter of *.WAV and a filename xxx without the mp3 extension will cause it to give you a filename of xxx.wav when you only click OK, thus xxx.mp3 -> xxx.wav

Caius Jard
  • 72,509
  • 5
  • 49
  • 80