C# 使用文件流从根目录文件夹读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12236114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:33:41  来源:igfitidea点击:

Read file from root directory folder using filestream

c#.netfilefile-io

提问by SurajSing

There are two Image files in my folder which I have to call in my program. I have used:

我的文件夹中有两个图像文件,我必须在我的程序中调用它们。我用过了:

AppDomain.curentDomain.baseDirectory + "Path and file name";

But this goes into my bin directory which I don't want; I want to read the folder from root directory where my folder name as resource I have saved my file there and call the image so please what's the code for that?

但这进入了我不想要的 bin 目录;我想从根目录读取文件夹,我的文件夹名称作为资源我已经在那里保存了我的文件并调用了图像,所以请问代码是什么?

How do I read from root directory in a Windows Form Application?

如何从 Windows 窗体应用程序中的根目录读取?

采纳答案by Kiran Solkar

Usually, you have to set those items to be copied into the bin folder. Right click in solution explorer/navigator, choose properties and set "Copy to output directory". hope this will work

通常,您必须将这些项目设置为复制到 bin 文件夹中。右键单击解决方案资源管理器/导航器,选择属性并设置“复制到输出目录”。希望这会奏效

回答by Scott

You can use this:

你可以使用这个:

System.IO.Path.Combine(Environment.CurrentDirectory, "Path to File")

Environment.CurrentDirectorywill give you the path that your application is being run from. It doesn't matter if it is being run within Visual Studio or whether your application is deployed.

Environment.CurrentDirectory将为您提供运行应用程序的路径。它是在 Visual Studio 中运行还是您的应用程序是否已部署都无关紧要。

Example Usage

示例用法

// Read image1.jpg from application folder, into Image object
Image myImage = Image.FromFile(System.IO.Path.Combine(Environment.CurrentDirectory, "image1.jpg"));

回答by RollingCog

System.IO.Path.Combine(Application.StartupPath, @"..\..\YourFile.JPG")

returns the absolute path to your file, but this will onlywork while you're working in VS because there is no bin\Debug when you deploy your app.

返回文件的绝对路径,但这在您在 VS 中工作时有效,因为在您部署应用程序时没有 bin\Debug。

string path = Path.Combine(Application.StartupPath, @"..\..\YourFile.JPG");
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream);
stream.Close();

If you plan on sending the file along with the exe, right click the file in Solution Explorer, select Include in project, right click again, select properties and set the Build Action : Noneand Copy to Output Directory : Copy if newerin the properties window, this will copy the file to your bin\Debug every time you build. Then you can use:

如果您计划将文件与 exe 一起发送,请在解决方案资源管理器中右键单击该文件,选择包含在项目中,再次右键单击,选择属性并设置构建操作:无复制到输出目录:如果属性中较新,则复制窗口,这将在您每次构建时将文件复制到您的 bin\Debug 中。然后你可以使用:

string path = Path.Combine(Application.StartupPath, "YourFile.JPG");

which will work in VS andwhen you deploy. Better yet embed the file as a resource in your executable for a cleaner deploy.

这将在 VS部署时工作。最好将该文件作为资源嵌入到您的可执行文件中,以便进行更清晰的部署。

回答by SkyRune

Why not use the Environment.CurrentDirectory?

为什么不使用Environment.CurrentDirectory

string path = Environment.CurrentDirectory + @"\Image1.jpg";
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream);
stream.Close();

It is a way better the combining the application startup path. :) Environment.CurrentDirectoryreturns the current path in where your Application resides.

结合应用程序启动路径是一种更好的方式。:)Environment.CurrentDirectory返回应用程序所在的当前路径。

回答by Ayson Baxter

Use Server.MapPath("/path/to/file")

Server.MapPath("/path/to/file")