winform教_电脑绝技教你22天学精Csharp之第十五天winform应用程序补充5
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace _10打開對話框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//點擊彈出對話框
OpenFileDialog ofd = new OpenFileDialog();
//設置對話框的標題
ofd.Title = "請選擇要打開的文本文件喲親 O(∩_∩)O~";
//設置對話框可以多選
ofd.Multiselect = true;
//設置對話框的初始目錄
ofd.InitialDirectory = @"C:甥敳獲SpringRainDesktop";
//設置對話框的文件類型
ofd.Filter = "文本文件|*.txt|媒體文件|*.wmv|圖片文件|*.jpg|所有文件|*.*";
//展示對話框
ofd.ShowDialog();
//獲得在打開對話框中選中文件的路徑
string path = ofd.FileName;
if (path == "")
{
return;
}
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
//實際讀取到的字節數
int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _10打開對話框
{
static class Program
{
///
/// 應用程序的主入口點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
11、保存文件對話框
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _11_保存文件對話框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "請選擇要保存的路徑";
sfd.InitialDirectory = @"C:甥敳獲SpringRainDesktop";
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
sfd.ShowDialog();
//獲得保存文件的路徑
string path = sfd.FileName;
if (path == "")
{
return;
}
using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
fsWrite.Write(buffer, 0, buffer.Length);
}
MessageBox.Show("保存成功");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _11_保存文件對話框
{
static class Program
{
///
/// 應用程序的主入口點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
總結
以上是生活随笔為你收集整理的winform教_电脑绝技教你22天学精Csharp之第十五天winform应用程序补充5的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 网络连接 网络是否可用,
- 下一篇: 大工14秋《计算机应用基础》在线测试2,