我正在使用 C# 和 Patagames/PDFium 库在我自己的 PDF 阅读器中工作,我可以使用“OpenFileDialog”打开文件并将它们显示在屏幕上。但是,由于老板的要求,我不允许屏幕上有任何按钮。我们只需要单击任何 .PDF 文件(例如,在此路径中:C:\Users\Adaptabilidad\Desktop\Test.pdf)并启动它并直接显示 PDF 文档,而无需查找文件的目录. 我已将我的“.exe”设置为默认应用程序,尽管执行了 PDF 阅读器,但没有显示 PDF 文件。
我在初始化组件后尝试了 Application.ExecutablePath、Application.StartUpPath 并且我仍然获得了我的 PDF 阅读器可执行文件(.Exe)的路径,但我需要知道要打开的文件是什么(文件路径)。
如何获取有关启动我的应用程序的 .pdf 文件(目录可能不同)的信息?如果有帮助,您可以在下面查看我的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Patagames;
using System.IO;
namespace aPDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "PDF Files (*.pdf)|*.pdf";
if (dialog.ShowDialog() == DialogResult.OK)
{
openfile(dialog.FileName);
}
}
public void openfile(string filepath)
{
byte[] bytes = System.IO.File.ReadAllBytes(filepath);
var stream = new MemoryStream(bytes);
Patagames.Pdf.Net.PdfDocument pdfDocument = Patagames.Pdf.Net.PdfDocument.Load(stream);
pdfViewer1.Document = pdfDocument;
}
}
}
更新:
我找到了办法。你们中的一个评论让我知道如何去做。
我在 Program.cs 中使用的是以下句子:
public static string[] cmdLine = Environment.GetCommandLineArgs();
public static string cmd = cmdLine[1];
然后,y 使用“cmd”作为文件路径。
为什么?Environment.GetCommandLineArgs(); 返回 2 个值,即您正在执行的 .exe(您的程序)和您用于启动该 .exe 的文件作为第二个值。
而已。谢谢您的回答。