简单问题:C/S结构中,选择路径保存文件的问题 在程序中要保存文件(文件任意,文本文件或者excel等),保存的路径由用户选择这个如何实现呢?有现成的控件可以用吗?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace WindowsApplication16{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.SaveFileDialog saveFileDialog1; private System.Windows.Forms.TextBox tbPath; private System.Windows.Forms.Button btnBrowse; private System.Windows.Forms.Button btnSave; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.tbPath = new System.Windows.Forms.TextBox(); this.btnBrowse = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button(); this.SuspendLayout(); // // tbPath // this.tbPath.Location = new System.Drawing.Point(40, 32); this.tbPath.Name = "tbPath"; this.tbPath.Size = new System.Drawing.Size(200, 21); this.tbPath.TabIndex = 0; this.tbPath.Text = ""; // // btnBrowse // this.btnBrowse.Location = new System.Drawing.Point(256, 32); this.btnBrowse.Name = "btnBrowse"; this.btnBrowse.TabIndex = 1; this.btnBrowse.Text = "浏 览"; this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click); // // btnSave // this.btnSave.Location = new System.Drawing.Point(256, 72); this.btnSave.Name = "btnSave"; this.btnSave.TabIndex = 2; this.btnSave.Text = "保 存"; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(488, 197); this.Controls.Add(this.btnSave); this.Controls.Add(this.btnBrowse); this.Controls.Add(this.tbPath); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void btnBrowse_Click(object sender, System.EventArgs e) { saveFileDialog1.AddExtension = true; saveFileDialog1.Filter = "Excel 文件 (*.xls)|*.xls|所有文件 (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if( saveFileDialog1.ShowDialog() == DialogResult.OK ) { tbPath.Text = saveFileDialog1.FileName; } } private void btnSave_Click(object sender, System.EventArgs e) { System.IO.StreamWriter sw = new System.IO.StreamWriter(tbPath.Text,false, System.Text.Encoding.Default); sw.WriteLine("保存 Excel!"); sw.Close(); MessageBox.Show("文件保存成功!"); } }} 五个三角???-----------------------俺是做B/S结构的,看来也得学学c/S下的了,这么简单的东西都不知道,真有些丢脸了呀谢谢楼上的提醒 installshield2008 out与ref的前世今生 form间传值 100分,一个简单的问题,在线等 怎么获取一个结点的深度(windows)(treeview) 水晶报表产生的临时文件问题,在线等!!! 如何解决调入数据出图的问题 oracle数据库如何连接?--在线等待 请问我怎么从Access里面取不到数据呢 设计WinForm用户控件时发生问题,请高手解答 使用COM组件问题 C#中,如果用户没有权限浏览,如何阻止页面的执行?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication16
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox tbPath;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.Button btnSave;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.tbPath = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tbPath
//
this.tbPath.Location = new System.Drawing.Point(40, 32);
this.tbPath.Name = "tbPath";
this.tbPath.Size = new System.Drawing.Size(200, 21);
this.tbPath.TabIndex = 0;
this.tbPath.Text = "";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(256, 32);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.TabIndex = 1;
this.btnBrowse.Text = "浏 览";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(256, 72);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 2;
this.btnSave.Text = "保 存";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 197);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.tbPath);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void btnBrowse_Click(object sender, System.EventArgs e)
{
saveFileDialog1.AddExtension = true;
saveFileDialog1.Filter = "Excel 文件 (*.xls)|*.xls|所有文件 (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true; if( saveFileDialog1.ShowDialog() == DialogResult.OK )
{
tbPath.Text = saveFileDialog1.FileName;
}
} private void btnSave_Click(object sender, System.EventArgs e)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(tbPath.Text,false, System.Text.Encoding.Default); sw.WriteLine("保存 Excel!");
sw.Close(); MessageBox.Show("文件保存成功!");
}
}
}
俺是做B/S结构的,看来也得学学c/S下的了,这么简单的东西都不知道,真有些丢脸了呀谢谢楼上的提醒