如何在不显示 SaveDialog 的提示的情况下将文件保存在特定位置

How to save files in specific locations without showing SaveDialog's Prompt

我一直想知道有没有办法在 C# 中将文件保存在特定的文件夹中?

我知道的唯一方法是 'SaveFileDialog' 但问题是我想保存文件

在文件夹中,不显示 saveFilesDialog 的框。

saveFilesDialog's Box : 是提示您单击 'YES' 或 'CANCEL' 的框。

代码示例

-形式1

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

    
}

期待结果 [1]: https://i.stack.imgur.com/9JqWO.png

谁能帮帮我?我有点卡住了 rn :)

这是我的完整代码,很难读懂,但你会明白重点的

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);
                            }

                        }
                    }
                    
                }

            }
           
            
        }

     
    }

这段代码工作得很好!!但是我每次都需要点击'OK'!! 所以有没有每次都不用点击'OK'就可以保存文件的方法!!

从概念上讲,SaveFileDialog 唯一要做的事情就是更改文件扩展名*。使用 Path.ChangeExtension 代替

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

wav 将与 mp3 一起保存。如果你想把它保存在别处,使用Path.Combine建立一个新路径例如

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

*我这样说是因为给它一个过滤器 *.WAV 和一个没有 mp3 扩展名的文件名 xxx 将导致它给你一个文件名 xxx.wav 当你只点击确定时, 因此 xxx.mp3 -> xxx.wav