C#_WinForm_打开一个进程、文件等.
2014-07-10 15:16
316 查看
在WinForm/C#中打开一个文件,主要是用到进程的知识。
下面是一些实例,可以模仿着去实现。
1. 打开文件
private
void btOpenFile_Click(objectsender,
EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfoinfo =
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(objectsender,
EventArgs e)
{
//启动IE进程
System.Diagnostics.Process.Start("IExplore.exe");
}
3. 打开指定URL
方法一:
private
void btOpenURL_Click(objectsender,
EventArgs e)
{
//启动带参数的IE进程
System.Diagnostics.Process.Start("IExplore.exe",
"http://hi.baidu.com/qinzhiyang");
}
方法二:
private
void btOpenURLwithArgs_Click(objectsender,
EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfostartInfo =
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(objectsender,
EventArgs e)
{
//获取“收藏夹”文件路径
string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
//启动进程
System.Diagnostics.Process.Start(myFavoritesPath);
}
5. 打印文件
private
void PrintDoc()
{
//定义一个进程实例
System.Diagnostics.ProcessmyProcess =
new System.Diagnostics.Process();
try
{
//设置进程的参数
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = myDocumentsPath+
"\\TxtForTest.txt";
myProcess.StartInfo.Verb = "Print";
//显示txt文件的所有谓词
foreach (stringv
in myProcess.StartInfo.Verbs)
MessageBox.Show(v);
myProcess.StartInfo.CreateNoWindow = true;
//启动进程
myProcess.Start();
}
catch (Win32Exceptione)
{
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.");
}
}
}
下面是一些实例,可以模仿着去实现。
1. 打开文件
private
void btOpenFile_Click(objectsender,
EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfoinfo =
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(objectsender,
EventArgs e)
{
//启动IE进程
System.Diagnostics.Process.Start("IExplore.exe");
}
3. 打开指定URL
方法一:
private
void btOpenURL_Click(objectsender,
EventArgs e)
{
//启动带参数的IE进程
System.Diagnostics.Process.Start("IExplore.exe",
"http://hi.baidu.com/qinzhiyang");
}
方法二:
private
void btOpenURLwithArgs_Click(objectsender,
EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfostartInfo =
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(objectsender,
EventArgs e)
{
//获取“收藏夹”文件路径
string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
//启动进程
System.Diagnostics.Process.Start(myFavoritesPath);
}
5. 打印文件
private
void PrintDoc()
{
//定义一个进程实例
System.Diagnostics.ProcessmyProcess =
new System.Diagnostics.Process();
try
{
//设置进程的参数
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = myDocumentsPath+
"\\TxtForTest.txt";
myProcess.StartInfo.Verb = "Print";
//显示txt文件的所有谓词
foreach (stringv
in myProcess.StartInfo.Verbs)
MessageBox.Show(v);
myProcess.StartInfo.CreateNoWindow = true;
//启动进程
myProcess.Start();
}
catch (Win32Exceptione)
{
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.");
}
}
}
相关文章推荐
- 在WinForm/C#中打开一个文件,主要是用到进程的知识。
- WinForm/C#中打开一个文件,主要是用到进程的知识。
- C#之WinForm基础 得到文件对话框(一次只能打开一个文件)打开文件的绝对路径
- 在WinForm/C#中打开一个文件
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- linux默认情况下,一个进程最多能打开多少文件?
- C#显示PDF文件,winform打开PDF文件并在窗体中显示
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- C#如何打开一个文件夹并定位到某个文件
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- C# WinForm打开PDF文件
- C# 开启一个新进程,IE,打开一个URL,第一次总失败,刷新一下就好了
- [导入]用c#打开一个外部文件
- C#让文件关联到自己做的WinForm,从文件打开WinForm
- C# 只能运行一个WinForm进程
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- 如何查看一个进程打开了哪些文件?
- C# 只能运行一个winForm进程
- 如何用c#打开文件夹并选择文件夹内的一个文件 open folder and select file