system.io.directorynotfound ->; 但它在控制台中有效

发布于 2024-07-30 01:36:33 字数 784 浏览 1 评论 0原文

我的文件是这样引用的(都是相对的):

// WHERE YOU KEEP THE PAGE TITLE XML
    public static string        myPageTitleXML = "xml/pagetitles.xml";

当我双击可执行文件时,我得到 system.io.directorynotfound和

    using (StreamReader r = new StreamReader(myPageTitleXML))
    { //etc.. . .etc....etc..
    }

“这个问题需要关闭”。 但从控制台运行它就像一个魅力。 这是怎么回事?

我尝试设置Environment.CurrentDirectory,但无法让任何东西发挥作用。 无论如何我为什么要这么做? 它违背了相对路径的目的不是吗?

响应.. .

“应用程序”在当前上下文中不存在,我将继续尝试人们提到的内容,这不是 windows.form

测试

Path.GetDirectoryName (Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML); 给出错误 不支持 URI 格式,Path.GetFullPath() 也是如此。 Server.MapPath 也会导致错误,当前处于离线状态

My files are referenced like so (it's all relative):

// WHERE YOU KEEP THE PAGE TITLE XML
    public static string        myPageTitleXML = "xml/pagetitles.xml";

and

    using (StreamReader r = new StreamReader(myPageTitleXML))
    { //etc.. . .etc....etc..
    }

I get system.io.directorynotfound, and "this problem needs to be shut down", when I double click the executable. But running it from the console works like a charm. What's wrong here?

I played around with attempting to set Environment.CurrentDirectory but couldn't get anything to work. Why should I have to do that anyway? It defeats the purpose of a relative path no?

responding.. .

"application" does not exist in the current context, i'll keep trying what people have mentioned, this is not a windows.form

testing

Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML); gives error URI formats are not supported, as does Path.GetFullPath(). Server.MapPath results in an error as well, this is currently offline

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请你别敷衍 2024-08-06 01:36:33

假设这个目录位于代码执行目录下的某个位置,听起来您可以使用 ..

Application.ExecutablePath() 

Application.StartUpPath()

.. 来了解应用程序在搜索“xml”目录时看到的内容其中的“pagetitles.xml”文件。

如果这些方法之一返回的目录没有指向您认为的位置,则您需要移动应用程序的位置或此文件夹的位置,以便它与应用程序位于同一目录中。

希望这能让您走上正确的道路。

Well assuming this directory is somewhere under the directory in which your code is executing, it sounds like you can use ..

Application.ExecutablePath() 

or

Application.StartUpPath()

.. to get an idea as to what your application is seeing when it goes in search of an 'xml' directory with the 'pagetitles.xml' file in it.

If the directory returned by one of these methods does not point where you thought it did, you'll need to move the location of your application or the location of this folder so that it is within the same directory as the app.

Hope this gets you on the right path.

蹲墙角沉默 2024-08-06 01:36:33

那么,当您通过双击可执行文件运行它时,名为 xml 的文件夹中是否有一个名为 pagetitles.xml 的文件,其中 xml 是与可执行文件位于同一位置的文件夹?

当然可以使用这样的相对路径,但我不会真正推荐它。 相反,也许使用类似的东西:

 string fileToOpen = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML);  
 using (StreamReader r = new StreamReader(fileToOpen))  
 {   
   //etc.. . .etc....etc..  
 }

So, when you run it from double clicking the executable, is there a file named pagetitles.xml in a folder named xml, where xml is a folder in the same location as the executable?

It's certainly possible to use relative paths like this, but I wouldn't really recommend it. Instead, maybe use something like:

 string fileToOpen = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML);  
 using (StreamReader r = new StreamReader(fileToOpen))  
 {   
   //etc.. . .etc....etc..  
 }
我喜欢麦丽素 2024-08-06 01:36:33

这是 ASP.NET 代码吗? 如果是这样,那么您可能需要执行 MapPath("xml/pagetitles.xml")

Is this ASP.NET code? If so then you probably need to do MapPath("xml/pagetitles.xml")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文