C# 如何从文件夹wpf获取文件路径?
我有一个问题,我正在开发一个WPF应用程序,其中有一个页面名为“帮助”,其中有一个按钮 当用户单击此按钮时,我必须向用户提供帮助文件夹中的pdf文件。 现在我的问题是,如果我这样写路径C# 如何从文件夹wpf获取文件路径?,c#,wpf,C#,Wpf,我有一个问题,我正在开发一个WPF应用程序,其中有一个页面名为“帮助”,其中有一个按钮 当用户单击此按钮时,我必须向用户提供帮助文件夹中的pdf文件。 现在我的问题是,如果我这样写路径 **string pdfurl1 = ((@"D:\addnkit\projects\wdine\widdne_working\Wdine Us\Wddine\Wine\Help\Emerald Wine Dispensing Software.pdf")); System
**string pdfurl1 = ((@"D:\addnkit\projects\wdine\widdne_working\Wdine Us\Wddine\Wine\Help\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);**
(@"pack://application:,,,/Widne;component/help/mypdf.pdf"
它成功地工作了
但我知道这在其他PC机上是行不通的,所以我想知道如何为任何PC机上都能运行的PC机编写代码
我也试过这样做
**string pdfurl1 = ((@"D:\addnkit\projects\wdine\widdne_working\Wdine Us\Wddine\Wine\Help\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);**
(@"pack://application:,,,/Widne;component/help/mypdf.pdf"
但它不起作用
[更新]
我已经尝试了所有的解决方案,但它还不起作用我不知道为什么?
请再查一遍
Widne>>帮助>>mypdf
您可以使用Directory.GetCurrentDirectory。这将为您提供启动应用程序的目录。因此,如果您将文件放在启动应用程序的位置,它将如下所示:
string pdfurl1 = ((Directory.GetCurrentDirectory() + "\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);
您可以使用Directory.GetCurrentDirectory。这将为您提供启动应用程序的目录。因此,如果您将文件放在启动应用程序的位置,它将如下所示:
string pdfurl1 = ((Directory.GetCurrentDirectory() + "\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);
第二种方法是packuri,你应该使用它
您的问题很可能是mypdf.pdf需要在Visual Studio中更改其属性,以便它在生成时实际复制pdf文件,而不是嵌入到应用程序中,从而阻止最终用户读取pdf文件
在pdf文件上设置这些属性并重新生成应用程序
构建操作:内容
复制到输出目录:始终复制
编辑:查看构建操作的解释。第二种方法packuri是您应该使用的
您的问题很可能是mypdf.pdf需要在Visual Studio中更改其属性,以便它在生成时实际复制pdf文件,而不是嵌入到应用程序中,从而阻止最终用户读取pdf文件
在pdf文件上设置这些属性并重新生成应用程序
构建操作:内容
复制到输出目录:始终复制
编辑:查看构建操作的解释。您可以使用相对路径:
./ - start at the .exe directory
../ - go up one folder from the .exe
../../ - go up two folders etc.
编辑:
假设您的.exe位于bin文件夹中:
**string pdfurl1 = ((@"..\Help\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);**
您可以使用相对路径:
./ - start at the .exe directory
../ - go up one folder from the .exe
../../ - go up two folders etc.
编辑:
假设您的.exe位于bin文件夹中:
**string pdfurl1 = ((@"..\Help\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);**
使用exe的路径
System.AppDomain.CurrentDomain.BaseDirectory
string path = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
+ "\\Widne\\component\\help\\mypdf.pdf";
System.Diagnostics.Process.Start(path )
更新:
在选择文件之前,他必须存在,在这种情况下,您将属性设置为PDF文件的“复制到输出目录”以始终复制,使用exe的路径
System.AppDomain.CurrentDomain.BaseDirectory
string path = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
+ "\\Widne\\component\\help\\mypdf.pdf";
System.Diagnostics.Process.Start(path )
更新:
在选择文件之前,他必须存在,在这种情况下,您可以将属性复制到PDF文件的“复制到输出目录”以始终复制也许来自的答案可以帮助您
使用该类建立您的路径。它会做正确的事情
对包含文件或目录的字符串实例执行操作
路径信息。这些操作在跨平台中执行
态度
也许我的答案可以帮助你
使用该类建立您的路径。它会做正确的事情
对包含文件或目录的字符串实例执行操作
路径信息。这些操作在跨平台中执行
态度
Processclass与WPF无关。如果路径是相对于应用程序的,则可以使用相对路径。如果您担心应用程序运行的工作目录与程序集存储的目录不同,则可以使用
Assembly.GetExecutingAssembly().CodeBase
并使用path.Combine构建PDF的路径。Diagnostics.Processclass与WPF无关。如果路径是相对于应用程序的,则可以使用相对路径。如果您担心应用程序运行的工作目录与程序集存储的目录不同,则可以使用
Assembly.GetExecutingAssembly().CodeBase
并使用path.Combine构建PDF的路径。对于目录对话框要获取目录路径,请首先添加reference System.Windows.Forms,然后解析,然后将此代码放入按钮单击中
var dialog = new FolderBrowserDialog();
dialog.ShowDialog();
folderpathTB.Text = dialog.SelectedPath;
folderpathTB是文本框的名称,我想在其中放置文件夹路径,或者您也可以将其分配给字符串变量,即
string folder = dialog.SelectedPath;
如果您想获得文件名/路径,只需单击按钮即可
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
folderpathTB是文本框的名称,我想在其中放置文件路径,或者您也可以将其分配给字符串变量
注意:对于文件夹对话框,必须将System.Windows.Forms.dll添加到项目中,否则它将无法工作。对于目录对话框,要获取目录路径,请先添加引用System.Windows.Forms,然后解析,然后单击按钮将此代码放入
var dialog = new FolderBrowserDialog();
dialog.ShowDialog();
folderpathTB.Text = dialog.SelectedPath;
folderpathTB是文本框的名称,我想在其中放置文件夹路径,或者您也可以将其分配给字符串变量,即
string folder = dialog.SelectedPath;
如果您想获得文件名/路径,只需单击按钮即可
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
folderpathTB是文本框的名称,我想在其中放置文件路径O
RU也可以将其分配给字符串变量
注意:对于文件夹对话框,必须将System.Windows.Forms.dll添加到项目中,否则它将无法工作。您也可以尝试使用以下代码:
string pdfFolder;
#if DEBUG
DirectoryInfo directoryInfo = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName);
pdfFolder = directoryInfo.FullName + @"\PdfFile\Auto Refill Reminder List.pdf";
System.Diagnostics.Process.Start(pdfFolder );
#endif
#if (!DEBUG)
pdfFolder = Environment.CurrentDirectory + @"\PdfFile\Auto Refill Reminder List.pdf";
Process.Start(pdfFolder);
#endif
并更改内容的生成类型:
您也可以尝试使用以下代码:
string pdfFolder;
#if DEBUG
DirectoryInfo directoryInfo = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName);
pdfFolder = directoryInfo.FullName + @"\PdfFile\Auto Refill Reminder List.pdf";
System.Diagnostics.Process.Start(pdfFolder );
#endif
#if (!DEBUG)
pdfFolder = Environment.CurrentDirectory + @"\PdfFile\Auto Refill Reminder List.pdf";
Process.Start(pdfFolder);
#endif
并更改内容的生成类型:
你的.exe在哪个文件夹中?在你的exe目录中的Widne?检查我的更新答案@akrem该目录将存在于uour调试目录中?此目录是否在您的主项目中?不,我没有向您显示调试目录尝试是此目录在主项目中.exe在哪个文件夹中?exe目录中的Widne?检查我的更新答案@akrem此目录将存在于uour调试目录中?这个目录在你的主项目中吗?不,我没有向你显示调试目录尝试是的,这个目录在主项目中请解释我更多检查更新我不知道为什么它仍然不工作我只是不能得到你就是这样请解释我更多检查更新我不知道为什么它仍然不工作我只是不能得到你那是allcheck更新了我不知道为什么它仍然不工作你需要得到目录名-CodeBase给你.exe文件。字符串path=path.CombinePath.GetDirectoryNameAssembly.GetExecutionGassembly.Location,@PrintTemplates\Promotions\images\promotion1.png;检查更新我不知道为什么它仍然不工作你需要得到目录名-CodeBase给你.exe文件。字符串path=path.CombinePath.GetDirectoryNameAssembly.GetExecutionGassembly.Location,@PrintTemplates\Promotions\images\promotion1.png;我不知道为什么还是不行working@dholakiyaankit请参见编辑。你的.exe在哪里?检查更新我不知道为什么它仍然不在working@dholakiyaankit请参见编辑。你的.exe在哪里?检查更新我不知道为什么它仍然不工作检查更新我不知道为什么它仍然不工作检查更新我不知道为什么它仍然不工作working@dholakiyaankit我更新了我的答案检查和回复检查更新我不知道为什么它仍然没有working@dholakiyaankit我更新我的答案,检查并回复