zoukankan      html  css  js  c++  java
  • 在WinForm/C#中打开一个文件

    在WinForm/C#中打开一个文件,主要是用到进程的知识。

    下面是一些实例,可以模仿着去实现。

    1.          打开文件

     1 private void btOpenFile_Click(object sender, EventArgs e)
     2 
     3 {
     4 
     5 //定义一个ProcessStartInfo实例
     6 
     7 System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
     8 
     9 //设置启动进程的初始目录
    10 
    11 info.WorkingDirectory = Application.StartupPath;
    12 
    13 //设置启动进程的应用程序或文档名
    14 
    15 info.FileName = @"test.txt";
    16 
    17 //设置启动进程的参数
    18 
    19 info.Arguments = "";
    20 
    21 //启动由包含进程启动信息的进程资源
    22 
    23 try
    24 
    25 {
    26 
    27 System.Diagnostics.Process.Start(info);
    28 
    29 }
    30 
    31 catch (System.ComponentModel.Win32Exception we)
    32 
    33 {
    34 
    35 MessageBox.Show(this, we.Message);
    36 
    37 return;
    38 
    39 }
    40 
    41 }

    2.          打开浏览器

    1 private void btOpenIE_Click(object sender, EventArgs e)
    2 
    3 {
    4 
    5 //启动IE进程
    6 
    7 System.Diagnostics.Process.Start("IExplore.exe");
    8 
    9 }

    3.          打开指定URL

    方法一:

    1 private void btOpenURL_Click(object sender, EventArgs e)
    2 
    3 {
    4 
    5 //启动带参数的IE进程
    6 
    7 System.Diagnostics.Process.Start("IExplore.exe", "http://hi.baidu.com/qinzhiyang");
    8 
    9 }

    方法二:

     1 private void btOpenURLwithArgs_Click(object sender, EventArgs e)
     2 
     3 {
     4 
     5 //定义一个ProcessStartInfo实例
     6 
     7 System.Diagnostics.ProcessStartInfo startInfo = newSystem.Diagnostics.ProcessStartInfo("IExplore.exe");
     8 
     9 //设置进程参数
    10 
    11 startInfo.Arguments = " http://hi.baidu.com/qinzhiyang ";
    12 
    13 //并且使进程界面最小化
    14 
    15 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
    16 
    17 //启动进程
    18 
    19 System.Diagnostics.Process.Start(startInfo);
    20 
    21 }

    4.          打开文件夹

     1 private void btOpenFolder_Click(object sender, EventArgs e)
     2 
     3 {
     4 
     5 //获取“收藏夹”文件路径
     6 
     7 string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
     8 
     9 //启动进程
    10 
    11 System.Diagnostics.Process.Start(myFavoritesPath);
    12 
    13 }

    5.          打印文件

     1 private void PrintDoc()
     2 
     3 {
     4 
     5 //定义一个进程实例
     6 
     7 System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
     8 
     9 try
    10 
    11 {
    12 
    13 //设置进程的参数
    14 
    15 string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    16 
    17 myProcess.StartInfo.FileName = myDocumentsPath + "\\TxtForTest.txt";
    18 
    19 myProcess.StartInfo.Verb = "Print";
    20 
    21 //显示txt文件的所有谓词
    22 
    23 foreach (string v in myProcess.StartInfo.Verbs)
    24 
    25 MessageBox.Show(v);
    26 
    27        
    28 
    29 myProcess.StartInfo.CreateNoWindow = true;
    30 
    31 //启动进程
    32 
    33 myProcess.Start();
    34 
    35 }
    36 
    37 catch (Win32Exception e)
    38 
    39 {
    40 
    41 if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
    42 
    43 {
    44 
    45 MessageBox.Show(e.Message + " Check the path." + myProcess.StartInfo.FileName);
    46 
    47 }
    48 
    49 else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
    50 
    51 {
    52 
    53 MessageBox.Show(e.Message + " You do not have permission to print this file.");
    54 
    55 }
    56 
    57 }
    58 
    59 }
  • 相关阅读:
    java.lang.Object中的方法
    lyt经典版MySQL基础——进阶3:排序查询
    lyt经典版MySQL基础——进阶5:分组查询
    lyt经典版MySQL基础——DML语言-数据的插入、修改、删除操作
    lyt经典版MySQL基础——进阶8:联合查询
    lyt经典版MySQL基础——进阶7:子查询
    lyt经典版MySQL基础——进阶6:连接查询-sql99语法-内连接、外连接、交叉连接
    lyt经典版MySQL基础——进阶6:连接查询-sql92语法-内连接
    lyt经典版MySQL基础——进阶4:常见函数-分组函数
    lyt经典版MySQL基础——进阶2:条件查询
  • 原文地址:https://www.cnblogs.com/pyffcwj/p/WinForm.html
Copyright © 2011-2022 走看看