Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#.net中读取windows应用程序根目录中的文件?_C#_.net_Winforms_Filestream_Windows Applications - Fatal编程技术网

如何在C#.net中读取windows应用程序根目录中的文件?

如何在C#.net中读取windows应用程序根目录中的文件?,c#,.net,winforms,filestream,windows-applications,C#,.net,Winforms,Filestream,Windows Applications,如果我使用Application.StartupPath或AppDomain.CurrentDomain.BaseDirectory 它会在bin\debug文件夹中搜索该文件 但是我必须在ma c#.net项目中使用根目录“Resources\imagefile.png”中的文件夹 Filestream fs; fs = new Filestream(AppDomain.CurrentDomain.BaseDirectory + "folder\\imagefile.png"); 所以,尽管

如果我使用Application.StartupPath或AppDomain.CurrentDomain.BaseDirectory 它会在bin\debug文件夹中搜索该文件 但是我必须在ma c#.net项目中使用根目录“Resources\imagefile.png”中的文件夹

Filestream fs;
fs = new Filestream(AppDomain.CurrentDomain.BaseDirectory + "folder\\imagefile.png");
所以,尽管使用了上面的代码或目录的完整路径“@C:…”,但我们如何编写代码从我的根目录读取文件,甚至Server.MapPath也是我们无法使用的

获取我的应用程序路径,但这给出了 C:\Projects\XYZ\ABC\bin\Debug

我不需要bin\Debug。有什么方法可以实现这一点吗


通常,您会将这些项目设置为复制到bin文件夹中。右键单击解决方案资源管理器/导航器,选择属性并使用Environment.CurrentDirectory将“复制到输出目录”设置为其他内容。

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

如果我将该文件副本设置为文件夹中的输出目录,那么我应该如何读取根文件夹!那么,如果我要为ma程序创建一个安装文件,那么我可以从ma程序中检索这些文件吗?你说的“根文件夹”是什么意思?你的.exe所在的文件夹?不,它在我运行解决方案的bin文件夹外的何处检查此问题:请查看:[[1]:我只需要太多的主应用程序文件夹,我就是这样计算出来的;字符串path=AppDomain.CurrentDomain.BaseDirectory.IndexOf(“bin”)+@“\ImageName.jpg”);使用
path.Combine(Environment.CurrentDirectory,“YourImage.jpg”)
代替。从VS2017 15.6
环境开始。CurrentDirectory
将返回可执行文件的目录。