当我执行file.getattributes()时,为什么我会收到消息'文件不存在' [英] Why do I get message 'file doesnt exist' when I do a file.getattributes()
问题描述
我有一个小型WPF应用程序,我请求用户在文本框中输入文件名以对该文件进行一些处理。当我在调试模式下运行VS2010以查看是否可以通过附加代码找到该文件时,即使我知道文件存在,我也会收到错误消息文件不存在!这是代码:
string PDFfile = System.IO.Path.GetFullPath( this .txtInputFileName.Text);
string strFileAttr =(File.GetAttributes(PDFfile).ToString());
if (!File.Exists(PDFfile))
{
MessageBox.Show(< span class =code-string> 此文件不存在!, 错误,MessageBoxButton.OK);
return ;
}
我的尝试:
文件PDF文件EXISTS在我运行VS2010的位置!
引用:我已经尝试将文件的完整位置硬编码为:
string PDFfile =C: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
你似乎错过了
string PDFfile = C:\\Users \\ERIC \\\ \\Desktop \\XML_Docs \\Praxair(Thailand)Co.,Ltd.pdf;
您应该将Windows资源管理器配置为始终显示文件扩展名以避免这样的问题:
在Windows 10 Windows 10教程中隐藏或显示文件扩展名 [ ^ ]
你还应该考虑使用一个OpenFileDialog
[ ^ 让用户选择文件,而不是依赖它们输入完整路径并正确命名。
引用:文件PDFfile EXISTS在我运行VS2010的位置!
检查你的应用程序的运行位置或提供一个完整路径的文件名。
要么你不在你认为的位置,要么就是文件真的机器人是否存在。
可执行文件通常位于< project directory> \bin\Debug
中,所以这就是它将检查PDF文件。如果那不是您放置文件的位置,则可以指定完整路径。
I have a small WPF application whereby I request the user to enter a file name in a Textbox to do some processing on that file. When I run VS2010 in Debug mode to see if the file can be located via the attached code, I get the error message 'File doesn't exist' even though I KNOW the file exists! Here's the code:
string PDFfile = System.IO.Path.GetFullPath(this.txtInputFileName.Text); string strFileAttr = (File.GetAttributes(PDFfile).ToString()); if (!File.Exists(PDFfile)) { MessageBox.Show("This file doesn't exist!", "Error", MessageBoxButton.OK); return; }
What I have tried:
The file PDFfile EXISTS in the location where I run VS2010!解决方案Quote:I've tried hard-coding the FULL location of the file as :
string PDFfile = "C:\\Users\\ERIC\\Desktop\\XML_Docs\\Praxair (Thailand) Co., Ltd.";
You seem to be missing the
string PDFfile = "C:\\Users\\ERIC\\Desktop\\XML_Docs\\Praxair (Thailand) Co., Ltd..pdf";
You should configure Windows Explorer to always show file extensions to avoid problems like this:
Hide or Show File Name Extensions in Windows 10 Windows 10 Tutorials[^]
You should also consider using anOpenFileDialog
[^] to let the user select the file, rather than relying on them to type in the full path and name correctly.
Quote:The file PDFfile EXISTS in the location where I run VS2010!
Check the location where your app is ran or give a filename with full path.
Either you are not where you think you are, or the file really does bot exist.
The executable will typically be located in<project directory>\bin\Debug
, so that's where it will check for the PDF file. If that's not where you put the file, you could specify the full path.
这篇关于当我执行file.getattributes()时,为什么我会收到消息'文件不存在'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!