using System;
using System.Diagnostics;
using System.ComponentModel;namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5; /// <summary>
/// Prints a file with a .doc extension.
/// </summary>
void PrintDoc()
{
Process myProcess = new Process();

try
{
// Get the path that stores user documents.
string myDocumentsPath = 
Environment.GetFolderPath(Environment.SpecialFolder.Personal); myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}  else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message + 
". You do not have permission to print this file.");
}
}
}
public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.PrintDoc();
}
}
}

解决方案 »

  1.   

    直接用Process.Start运行程序就可以了,操作系统会自己去判断是否需要用关联的程序去打开的。
      

  2.   

    直接用Process.Start的话,没办法打开程序不关联的文件,对这样的文件要求调用文件打开方式对话框,让用户自己选择改怎么打开。如某些txt备份文件后缀名为bak文件,可以让用户选择用文本方式打开。
      

  3.   

    只想实现的是打开时,没关联的调用windows打开方式对话框就可以啦。
      

  4.   

    做安装包时
    添加文件类型这样安装时就会去注册表注册文件关联。
    当然,你也可以手动注册。在程序Main方法中获取打开文件的路径就OK了。
      

  5.   

    如果这样的话,你必须去判断文件是否有关联了的
    使用Process.Start打开无关联文件,是会报错的
      

  6.   

    打开某文件时,如果有相关联的程序是就用关联的程序打开,无关联时调用打开方式对话框,让用户选择文件打开方式。-----------------------------------
    以上描述是Windows的系统帮助吧!
    不用作任何操作,windows本身就是这样的啊!如你你安装了photoshop 那么双击*.psd就会自动运行Photoshop。没有安装那么就要选择文件打开方式。不太明白LZ的意思。
    楼上的意思也不太明白。
      

  7.   

    如果能知道Windows怎么打开文件,那就解决了
      

  8.   

    改程序是不直接打开文件的,只是给出个文件名,如果有和该类型的文件关联的程序就调用程序打开,如打开xxx.doc,在word中打开xxx.doc。如果没有关联如xxx.bbb 这种文件就打开方式对话框,交给用户处理。与windows中双击打开一个文件一样,只不过我的程序中需要给出一个文件名而已,不需要双击。
    这下应该说明白了吧,再不明白我就死了。