簡體   English   中英

將文件保存到 Windows Form C# 中的特定文件夾

[英]Save file to a specific folder in Windows Form C#

我正在嘗試將一些選定的文件保存在我的應用程序內的文件夾(圖像)中在此處輸入圖片說明

我可以使用以下代碼獲取文件:

         private void button1_Click(object sender, EventArgs e)
    {

        string imagelocation = "";

        OpenFileDialog dialog = new OpenFileDialog();

        if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK )
        {
            textBox2.Text = dialog.FileName;
        }
    }

為了保存我在 textBox2 中獲得的文件,我嘗試了以下代碼。 但是使用以下代碼,我還必須選擇要保存文件的路徑。 如果我想(將我的路徑永久設置為“圖像”文件夾,如圖所示)進行保存怎么辦?

       private void button2_Click(object sender, EventArgs e)
    {


        SaveFileDialog f = new SaveFileDialog();

        if(f.ShowDialog() == DialogResult.OK)
        {
            using(Stream s = File.Open(f.FileName, FileMode.CreateNew))
            using(StreamWriter sw =  new StreamWriter(s))
            {
                sw.Write(textBox2.Text);
            }
        }
     }

2 解決這個問題的方法


  • 第一種方法:(瀏覽文件並點擊保存,將選中的文件自動保存到圖像目錄)


    private void button2_Click(object sender, System.EventArgs e)
    {

        var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        var assemblyParentPath = Path.GetDirectoryName(assemblyPath);
        var imageDir = Path.Combine(assemblyParentPath, "Image");

        if (!Directory.Exists(imageDir))
            Directory.CreateDirectory(imageDir);


         using (Stream s = File.Open(imageDir+"\\"+Path.GetFileName(textBox1.Text), FileMode.CreateNew))
         using (StreamWriter sw = new StreamWriter(s))
         {
                     sw.Write(textBox1.Text);
         }

     }


  • 第二種方法:(瀏覽文件並保存以目錄作為圖像目錄和文件名作為先前選擇的文件打開SaveDialog)
private void button2_Click(object sender, System.EventArgs e)
        {

            var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var assemblyParentPath = Path.GetDirectoryName(assemblyPath);
            var imageDir = Path.Combine(assemblyParentPath, "Image");

            if (!Directory.Exists(imageDir))
                Directory.CreateDirectory(imageDir);

            SaveFileDialog f = new SaveFileDialog();
            f.InitialDirectory = imageDir;
            f.FileName = textBox1.Text;
            if (f.ShowDialog() == DialogResult.OK)
            {
                using (Stream s = File.Open(imageDir + "\\" + Path.GetFileName(textBox1.Text), FileMode.CreateNew))
                using (StreamWriter sw = new StreamWriter(s))
                {
                    sw.Write(textBox1.Text);
                }

            }
        }```

這些代碼在你那正常嗎?

private void button1_Click(object sender, EventArgs e)
{
    var dlg = new OpenFileDialog();
    dlg.Title = "Select Picture";
    dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
    dlg.Filter = "Common Picture Files|*.gif;*.png;*.bmp;|All Files|*.*";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = dlg.FileName;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM