积极答复者
C#保存到相对路径

问题
-
如何把文件保存到相对路径上,比如别人安装了我的程序,然后里面有一个文件夹,保存的文件都默认保存到那个文件夹里
- 已移动 Sheng Jiang 蒋晟Moderator 2009年7月4日 14:27 软件设计问题 (发件人:Visual C#)
答案
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace X.WinFormsApp { public partial class X200907042202 : Form { private Button btnSaveFile; private const String ImageDirectory = "Images"; // 保存图片的文件夹名 public X200907042202() { InitializeComponent(); this.InitClass(); } private void InitClass() { this.btnSaveFile = new Button(); this.btnSaveFile.Text = "选择并保存图片"; this.btnSaveFile.Click += new EventHandler(btnSaveFile_Click); this.Controls.Add(this.btnSaveFile); } private void btnSaveFile_Click(object sender, EventArgs e) { using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "所有文件|*.*"; if (dialog.ShowDialog() == DialogResult.OK) { string fSourceFilePath = dialog.FileName; // 原始文件路径 string fApplicationPath = AppDomain.CurrentDomain.BaseDirectory; // 应用程序所有的路径 string fImageDirectoryPath = Path.Combine(fApplicationPath, ImageDirectory); // Images 文件夹的完整路径 string fImagePath = Path.Combine(fImageDirectoryPath, Path.GetFileName(fSourceFilePath)); // 图片的新路径 if (!Directory.Exists(fImageDirectoryPath)) Directory.CreateDirectory(fImageDirectoryPath); if (!File.Exists(fImagePath)) File.Copy(fSourceFilePath, fImagePath); } } } }
}
知识改变命运,奋斗成就人生!- 已标记为答案 流氓班长 2009年7月5日 12:23
全部回复
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace X.WinFormsApp { public partial class X200907042202 : Form { private Button btnSaveFile; private const String ImageDirectory = "Images"; // 保存图片的文件夹名 public X200907042202() { InitializeComponent(); this.InitClass(); } private void InitClass() { this.btnSaveFile = new Button(); this.btnSaveFile.Text = "选择并保存图片"; this.btnSaveFile.Click += new EventHandler(btnSaveFile_Click); this.Controls.Add(this.btnSaveFile); } private void btnSaveFile_Click(object sender, EventArgs e) { using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "所有文件|*.*"; if (dialog.ShowDialog() == DialogResult.OK) { string fSourceFilePath = dialog.FileName; // 原始文件路径 string fApplicationPath = AppDomain.CurrentDomain.BaseDirectory; // 应用程序所有的路径 string fImageDirectoryPath = Path.Combine(fApplicationPath, ImageDirectory); // Images 文件夹的完整路径 string fImagePath = Path.Combine(fImageDirectoryPath, Path.GetFileName(fSourceFilePath)); // 图片的新路径 if (!Directory.Exists(fImageDirectoryPath)) Directory.CreateDirectory(fImageDirectoryPath); if (!File.Exists(fImagePath)) File.Copy(fSourceFilePath, fImagePath); } } } }
}
知识改变命运,奋斗成就人生!- 已标记为答案 流氓班长 2009年7月5日 12:23