Process.Start()的替代方案

时间:2010-01-14 16:17:56

标签: c# file-io process

我刚刚完成文档存储解决方案的编码,我遇到了以下问题。在UI中,用户可以按下按钮打开文件:

try
{
    Process.Start(file);
}
catch (Exception ex)
{
    //Error handling code
}

我的问题是,如果用户没有与文件类型相关联的应用程序,则会抛出一个componentmodel异常,并显示相应的消息。

我宁愿做的是在这种情况下弹出“打开方式”对话框,是否有方法调用我不知道?

5 个答案:

答案 0 :(得分:5)

在调用Process.Start之前,您可以检查注册表以查看是否有与该文件类型关联的应用程序。或者,您可以捕获componentmodel异常并从那里打开open对话框。

答案 1 :(得分:3)

有关使用“打开方式”对话框

,请参阅此文章

http://www.codeproject.com/KB/shell/openwith.aspx

我会将Process.Start调用放在try语句中,然后在catch中显示“Open With”。

答案 2 :(得分:1)

不,没有。我认为你目前的方法是最好的。只需尝试运行程序,然后在异常的情况下,由于文件没有关联,打开一个对话框,允许他们选择一个文件来运行程序。

答案 3 :(得分:1)

Process.Start("explorer.exe",file) 

也可能值得一试。

解决了我在XP,Vista和7中打开网址的问题

答案 4 :(得分:0)

如果为ASP.NET构建它,请使用下面的代码来避免“访问被拒绝”错误

HTML CODE:
<div class="container body">
                    <div class="report_main_container">
                      <div class="table-responsive table-responsive-lg text-center new-rprt-chgs loading-container">
                        <div class="loading-over" id="loading-over">
                          <img src="loading.gif">
                        </div>

                        <table class="table table-striped" id="main_report_table" style="display: none">
                          <thead id="fixedHeader"></thead>
                          <tbody></tbody>
                        </table>

                      </div>
                    </div>
                  </div>


JS CODE:
          var row_head = '';
        var row_head_reporte = [];
        row_head += '<tr>';

        for (let i = 0; i < my_report["columns"].length; i++) {
            // console.log(my_report["columns"][i]);
            if (my_report["columns"][i]["VISIBLE"] == "YES") {
                row_head += '<th onclick="sortTable('+ i +')">' + my_report["columns"][i]["NAME"] + '</th>';


            }
        }

        row_head += '</tr>';

        $("#main_report_table thead").append(row_head);


    function sortTable(n) {

        $(".loading-over").show();// This never fires!!!!

        var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
        table = document.getElementById("main_report_table");
        switching = true;
        // Set the sorting direction to ascending:
        dir = "asc";
        /* Make a loop that will continue until
        no switching has been done: */
        while (switching) {
          // Start by saying: no switching is done:
          switching = false;
          rows = table.rows;
          /* Loop through all table rows (except the
          first, which contains table headers): */
          for (i = 1; i < (rows.length - 1); i++) {
            // Start by saying there should be no switching:
            shouldSwitch = false;
            /* Get the two elements you want to compare,
            one from current row and one from the next: */
            x = rows[i].getElementsByTagName("TD")[n];
            y = rows[i + 1].getElementsByTagName("TD")[n];
            /* Check if the two rows should switch place,
            based on the direction, asc or desc: */
            if (dir == "asc") {
              if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
                // If so, mark as a switch and break the loop:
                shouldSwitch = true;
                break;
              }
            } else if (dir == "desc") {
              if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
                // If so, mark as a switch and break the loop:
                shouldSwitch = true;
                break;
              }
            }
          }
          if (shouldSwitch) {
            /* If a switch has been marked, make the switch
            and mark that a switch has been done: */
            rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
            switching = true;
            // Each time a switch is done, increase this count by 1:
            switchcount ++;
          } else {
            /* If no switching has been done AND the direction is "asc",
            set the direction to "desc" and run the while loop again. */
            if (switchcount == 0 && dir == "asc") {
              dir = "desc";
              switching = true;
            }
          }
        }
        $(".loading-over").hide();
    }