C#winfrom打开指定的文件
直接打开指定的文件
System.Diagnostics.Process.Start(v_OpenFilePath);
直接打开目录
string v_OpenFolderPath = @"目录路径"; System.Diagnostics.Process.Start("explorer.exe", v_OpenFolderPath);
在WinForm/C#中打开一个文件,主要是用到进程的知识。
下面是一些实例,可以模仿着去实现。
1. 打开文件
private void btOpenFile_Click(object sender, EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
//设置启动进程的初始目录
info.WorkingDirectory = Application.StartupPath;
//设置启动进程的应用程序或文档名
info.FileName = @"test.txt";
//设置启动进程的参数
info.Arguments = "";
//启动由包含进程启动信息的进程资源
try
{
System.Diagnostics.Process.Start(info);
}
catch (System.ComponentModel.Win32Exception we)
{
MessageBox.Show(this, we.Message);
return;
}
}
2. 打开浏览器
private void btOpenIE_Click(object sender, EventArgs e)
{
//启动IE进程
System.Diagnostics.Process.Start("IExplore.exe");
}
3. 打开指定URL
方法一:
private void btOpenURL_Click(object sender, EventArgs e)
{
//启动带参数的IE进程
System.Diagnostics.Process.Start("IExplore.exe", "http://hi.baidu.com/qinzhiyang");
}
方法二:
private void btOpenURLwithArgs_Click(object sender, EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfo startInfo = newSystem.Diagnostics.ProcessStartInfo("IExplore.exe");
//设置进程参数
startInfo.Arguments = " http://hi.baidu.com/qinzhiyang ";
//并且使进程界面最小化
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
//启动进程
System.Diagnostics.Process.Start(startInfo);
}
4. 打开文件夹
private void btOpenFolder_Click(object sender, EventArgs e)
{
//获取“收藏夹”文件路径
string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
//启动进程
System.Diagnostics.Process.Start(myFavoritesPath);
}
5. 打印文件
private void PrintDoc()
{
//定义一个进程实例
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
try
{
//设置进程的参数
string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = myDocumentsPath + "\\TxtForTest.txt";
myProcess.StartInfo.Verb = "Print";
//显示txt文件的所有谓词
foreach (string v in myProcess.StartInfo.Verbs)
MessageBox.Show(v);
myProcess.StartInfo.CreateNoWindow = true;
//启动进程
myProcess.Start();
}
catch (Win32Exception e)
{
if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
MessageBox.Show(e.Message + " Check the path." + myProcess.StartInfo.FileName);
}
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
MessageBox.Show(e.Message + " You do not have permission to print this file.");
}
}
}
C#winfrom打开指定的文件的更多相关文章
- Qt 打开指定的文件
最近项目用到使用本地的office打开指定的文件,记录一下代码: QString fileName = QFileDialog::getOpenFileName(this, tr("Open ...
- c#基础学习(0628)之使用进程打开指定的文件、模拟磁盘打开文件
使用进程打开指定的文件 模拟磁盘打开文件 class Program { static void Main(string[] args) { while(true) { Console.WriteLi ...
- [Android]用图库打开指定的文件夹,没错是第一个画面直接是图库的文件夹画面
参考了这个里面的代码 http://bbs.csdn.net/topics/380084274 一直报错 06-16 23:58:50.698 26148-26161/com.example.myap ...
- C#打开指定路径文件对话框
private string OpenFileDlog(string DeafultDir) { OpenFileDialog Ofd = new OpenFileDialog(); Ofd.AddE ...
- 如何快速在命令提示符(cmd)中打开指定的文件夹路径!
按住shift键然后 右击,如图:
- 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议
[源码下载] 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议 作者:webabcd 介绍与众不同 windows ...
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
原文:重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议 [源码下载] 重新想象 Windows 8 Store ...
- .Net ASP.NET 打开指定文件夹
比如要打开指定的文件夹,而不是弹出对话框 System.Diagnostics.Process.Start(@"D:\"); 这样就打开了D盘,和正常打开D盘是一样的.
- vmware安装无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件
刚刚安装好了虚拟机,Windows XP 64bit Professional,安装好了开发环境,然后重启机器后虚拟机就打不开了,提示“vmware安装无法打开内核设备 \\.\Global\vmx8 ...
随机推荐
- windows服务隐藏后门之克隆帐号
windows服务隐藏后门之克隆帐号 1.CMD命令行下,建立了一个用户名为“test$”,密码为“abc123!”的简单隐藏账户,并且把该隐藏账户提升为了管理员权限. PS:CMD命令行使用&quo ...
- MySQL 插入记录时自动更新时间戳
将字段设置成timestamp类型,同时默认值设置成 CURRENT_TIMESTAMP.
- spring源码系列7:Spring中的InstantiationAwareBeanPostProcessor和BeanPostProcessor的区别
概念 Bean创建过程中的"实例化"与"初始化"名词 实例化(Instantiation): 要生成对象, 对象还未生成. 初始化(Initialization ...
- Vue中Class与Style如何动态绑定
Class 可以通过对象语法和数组语法进行动态绑定: 对象语法: <div v-bind:class="{ active: isActive, 'text-danger': hasEr ...
- Pycharm中Python Console与Terminal的区别
1.Python Console是Python交互式模式,可以直接输入代码,然后执行,并立刻得到结果 2.Terminal是命令行模式,与系统的CMD(命令提示符)一样,可以运行各种系统命令
- kaldi使用cvte模型进行语音识别
操作系统 : Unbutu18.04_x64 gcc版本 :7.4.0 该模型在thch30数据集上测试的错误率只有8.25%,效果还是不错的. 模型下载地址: http://www.kaldi-as ...
- F#周报2019年第43期
新闻 F# eXchange 2020--征文通知 FSSF在忙什么?2019年第三季度版本 Miguel强烈推荐使用TensorFlow.NET 运行在ASP.NET Core 3上的SAFE-Bo ...
- 恶意代码检测工具 -- Mathematics Malware Detected Tools
Mathematics Malware Detected Tools 重要:由于缺少测试数据,部分结论可能不正确.更多更准确的结论,还需要进行大量实验. 概述 mmdt(Mathematics Mal ...
- 巨杉Tech | SequoiaDB数据域及存储规划
1 背景近年来,企业的各项业务发展迅猛,客户数目不断增加,后台服务系统压力也越来越大,系统的各项硬件资源也变得非常紧张.因此,在技术风险可控的基础上,希望引入大数据技术,利用大数据技术优化现有IT系统 ...
- Debian Buster 使用Lxde在界面中打开url提示错误解决
问题复现 这里我在VLC上看到个链接地址,蓝字部分,正常点击会跳转浏览器打开 可是 问题原因 非浏览器上点击url默认会使用xdg-open打开url 这里Debian Buster打包的时候,xdg ...