如何在位于此文件后面的文件夹上获取文件? [英] How to take the file on folder that located behind of this file?

查看:103
本文介绍了如何在位于此文件后面的文件夹上获取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,我有一个关于C#编程的问题..我有3个文件夹:



1.应用

2. bin

3.调试



我想让这个应用程序动态变为动态,所以我使用System.IO.Directory.GetCurrentDirectory()。它指向调试文件夹上的文件。

问题是,实际上我想在Application文件夹上取文件,所以我应该撤退两次,直到我可以到达Application文件夹。如何在Application文件夹上获取文件?谢谢。

Hi Guys, I got a problem about C# programming.. I have 3 folder :

1. Apllication
2. bin
3. Debug

I want to make this application become dynamically, so I use System.IO.Directory.GetCurrentDirectory(). It toward to file on the Debug Folder.
The problem is, actually I want to take file on the Application folder, so I should retreat twice until I can reach Application folder. How can I take file on Application folder? Thanks.

推荐答案

不要为此使用GetCurrentDirectory。

当前目录可以随程序运行而改变。如果你使用任何文件或文件夹对话框,那些会改变当前目录。



相反,使用众所周知的,不变的起始路径,例如 Application.StartupPath 或者:

Don't ever use GetCurrentDirectory for this.
The "current" directory can change as your program runs. If you use any of the File or Folder dialogs, those change the current directory.

Instead, use well-known, unchanging starting paths, such as Application.StartupPath or this:
Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
string folderPath = Path.GetDirectoryName(uri.LocalPath);


如你所说,撤退两次。 见这里 [ ^ ]



一般情况下,如果您不打算从生产中的两个步骤中取出文件,则不应将其置于测试中。相反,要么将文件复制到调试文件夹,要么将路径放在某个配置/设置中,以便以后可以更改它而不更改代码。



如果有帮助请花时间接受解决方案。谢谢。
As you said, retreat twice. See here[^]

In general, if you're not planning on taking the file from two steps back in production, you shouldn't put it in test. Instead, either copy the file to debug folder or put the path in some config / setting so you can change it later without changing code.

If this helps please take time to accept the solution. Thank you.


您是否在描述Visual Studio为WinForms项目自动生成的应用程序结构?如果是这样,文件夹中有三个文件夹bin / obj / properties,其名称与应用程序名称相同,该文件夹位于包含.sln文件和.suo文件的应用程序名称的文件夹中。



当然,可以通过多种方式创建其他文件夹:例如,你可以更改Application.StartupPath。



有很多(太多?)方法来定位当前正在执行的程序集/ app:
Are you describing an app structure generated automatically by Visual Studio for a WinForms project ? If so there are three folders bin/obj/properties inside a folder whose name is the same as the application name which is inside a folder with the application name that contains the .sln file, and .suo file.

Of course, you could have created other folders in a variety of ways: you could have changed the Application.StartupPath, for example.

There are many (too many ?) ways to locate the current executing assembly/app:
List<string> dotNetPaths = new List<string>
{
    Application.StartupPath,
    System.IO.Path.GetDirectoryName
         (System.Reflection.Assembly.GetExecutingAssembly().Location),
    AppDomain.CurrentDomain.BaseDirectory,
    System.IO.Directory.GetCurrentDirectory(),
    Environment.CurrentDirectory,
    System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase),
    System.IO.Path.GetDirectoryName(Application.ExecutablePath),
};</string></string>

注意:我从这里获取此列表:[ ^ ]。



获取在Visual Stu中以标准方式创建的正在运行的WinForm应用程序的最顶层目录dio(我正在使用2013):

Note: I took this list from here: [^].

To get the top-most directory of a running WinForm app created in the "standard" way in Visual Studio (I'm using 2013):

DirectoryInfo dir = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent;
string dirPath = dir.FullName;


这篇关于如何在位于此文件后面的文件夹上获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆