当我执行file.getattributes()时,为什么我会收到消息'文件不存在'

编程入门 行业动态 更新时间:2024-10-26 19:29:06
本文介绍了当我执行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( 此文件不存在!, 错误,MessageBoxButton.OK); return ; }

我的尝试: 文件PDF文件EXISTS在我运行VS2010的位置!

解决方案

引用:

我已经尝试将文件的完整位置硬编码为:

string PDFfile =C: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 你似乎错过了 .pdf 扩展名:

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 .pdf extension:

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 an OpenFileDialog[^] 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()时,为什么我会收到消息'文件不存在'

本文发布于:2023-10-22 08:40:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我会   当我   不存在   消息   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!