簡體   English   中英

Process.Start()中的錯誤 - 系統找不到指定的文件

[英]Error in Process.Start() – The System cannot find the file specified

我正在使用下面的代碼從數據庫中獲取文件並使用Dropdownlist中填充的Installed Printers中選定的打印機進行打印,我的問題是在使用Printjob.Start()期間拋出異常系統無法找到指定的文件

我的代碼是,

protected void ggvqpdetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.ToUpper().ToString() == "PRINTREC")
    {
        try
        {
            // Set the printer to a printer in the dropdown box when the selection changes. 
            PrintDocument printDoc = new PrintDocument();
            string a = TextBox1.Text + TextBox2.Text + TextBox3.Text;
            DataSet ds = ExamManagement.SP.Eval_QP_PrintSelect(a).GetDataSet();
            if (ddlprint.SelectedIndex != -1 && ds.Tables[0].Rows.Count > 0)
            {
                // The dropdown box's Text property returns the selected item's text, which is the printer name.
                printDoc.PrinterSettings.PrinterName = ddlprint.Text;

                Process printJob = new Process();
                printJob.StartInfo.FileName = ds.Tables[0].Rows[0]["Data"].ToString();
                printJob.StartInfo.UseShellExecute = true;
                printJob.StartInfo.Verb = "printto";
                printJob.StartInfo.CreateNoWindow = true;
                printJob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                printJob.StartInfo.Arguments = ddlprint.Text;
                printJob.StartInfo.WorkingDirectory = Path.GetDirectoryName(ds.Tables[0].Rows[0]["Data"].ToString());
                printJob.Start();
            }
        }
        catch(Exception ex)
        {
            Lblmsg.Visible = true;
            Lblmsg.Text = ex.Message;
        }
    }
}

顯然問題取決於你實際放在printJob.StartInfo.FileName 價值來自數據庫,因此唯一能夠調查它的人就是 ds.Tables[0].Rows[0]["Data"]查看您擁有的文件名,並確保該文件存在,並且您可以在本地客戶端上訪問,並嘗試從中進行打印。 當然,這也揭示了您的解決方案的弱點,該解決方案似乎在數據庫中存儲文件名,並期望該名稱是每個客戶端上的有效本地文件。 不太可能是真的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM