从两个项目形式相同的解决方案访问相同的文件 [英] Accessing Same file from two project form same solution

查看:108
本文介绍了从两个项目形式相同的解决方案访问相同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我正在Windows应用程序上工作.我有一个用于配置的xml文件,在同一个解决方案中添加了两个项目,我想使用相对路径从两个项目访问相同的xml文件.我该怎么做

我有xml文件名TeacherCongig.xml,已在第二个项目中将此文件添加为链接,并且我正在使用
此代码用于访问链接的文件

字符串appPath = Path.GetDirectoryName(Application.ExecutablePath);
ds.ReadXml(appPath +"\\ AppConfig \\ TeacherConfig.xml");

提供未找到的异常文件.



解决方案

您始终可以将同一文件作为快捷链接"添加到另一个项目中. 添加现有文件时,只需说添加为快捷方式"即可.

无论您在第一个文件中进行的更改是什么,然后显然都会包含在第二个项目中.

操作如下:使用添加" ...现有项目"添加文件.该文件将被复制到某个解决方案文件夹中.如果您使用新建项目",则会创建它.

在另一个项目中,执行添加" ...现有项",就像刚才一样打开它,但是在打开文件"对话框中,添加"按钮有两个由小箭头控制的选项:添加"或添加为链接".选择添加为链接".该文件在逻辑上将位于两个项目中,但是在一个项目中,它将通过相对于另一个项目文件的相对路径作为一个相对文件名(相对于项目文件)添加到另一个文件中(作为链接的外部"文件). .从物理上讲,这是同一文件.完全按计划进行. :-)


—SA


为什么不仅仅使用Application.StartupPath作为AppPath

 ds.ReadXml(Application.StartupPath + " ); 


Hi!

I am working on windows application . I have an xml file for configuration ,I have added two project in the same solution ,I want to access the same xml file from the both project by using relative path . How I can do this

I have xml file name TeacherCongig.xml, I have added this file as a link in the second project and i am using
this code for accessing the linked file

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
ds.ReadXml(appPath + "\\AppConfig\\TeacherConfig.xml");

give exception file not found.



Please help me!

解决方案

You can always add the same file to the other project as "a shortcut link".
When you add an existing file, just say add as shortcut.

Whatever changes you make in the first file then obviously get included in the second project.


Same solution or not — does not essentially matter (but better be neatly positioned withing the solution sub-directory.

Here is what to do: add a file using "Add"... "Existing item". The file will be copied is some solution folder. If you use "New Item", it will be created.

In another project, do "Add"... "Existing item", open it as you just did, but while in a Open File dialog, the button "Add" has two options controlled by a small arrow: "Add" or "Add as Link". Select "Add as Link". The file logically will be in two projects, but in one project it will be added as a relative file name (relative to the project file), in another one — as a linked "external" file, by relative path relative to another project file. Physically, this is the same file. Exactly as planned. :-)


—SA


why not just use Application.StartupPath as the AppPath

ds.ReadXml(Application.StartupPath + "\\AppConfig\\TeacherConfig.xml");


这篇关于从两个项目形式相同的解决方案访问相同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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