简体   繁体   中英

Relative paths with WinForm and ASP.NET app

I am writing an application that contains two projects in it. One project is the client app, which is a ASP.NET web app, and the other is the admin app that runs on WinForms (C# 3.5). Both projects reference a class that contains an XML file. The problem is that, short of hardcoding the directory of the XML file into that class, I'm having trouble seeing how to point to it's running directory.

The web app looks in c:\\windows\\system32 (directory of the asp dll), and the winforms app looks in the ~\\bin\\debug directory of the project.

How can I form a relative path to the XML file, given that it's different depending on which app is referencing it?

If you modify the class referencing the XML file to take in a parameter that tells it where the XML file resides, then you can have each app pass in its own location to that class as a parameter.

Let's say your class is called YourClass and its load XML function is called LoadXML.

For the web app, you would use:

YourClass.LoadXML(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath);

For the WinForms app, you would use:

YourClass.LoadXML(System.Windows.Forms.Application.StartupPath);

Inside YourClass, you would use that parameter as the absolute path, and append a relative path using System.IO.Path.Combine.

I know when working with WinForms you can use System.IO.Path.GetFullPath(string) to return you the absolute path from a relative path.

I am not super familiar on web, but perhaps this could help? http://blog.dotnetclr.com/archive/2009/06/08/506.aspx

Hope this helps.

You could add the xml file to one of the projects with build action set to content, and have a script that copies the xml file from one project (if newer) to the other project with build action set to content. You can use a post-build event to copy the xml file using a batch file (copyifnewer.bat):

@echo off
echo Comparing two files: %1 with %2

if not exist %1 goto File1NotFound
if not exist %2 goto File2NotFound

fc %1 %2 
if %ERRORLEVEL%==0 GOTO NoCopy

echo Files are not the same.  Copying %1 over %2
copy %1 %2 /y & goto END

:NoCopy
echo Files are the same.  Did nothing
goto END

:File1NotFound
echo %1 not found.
goto END

:File2NotFound
copy %1 %2 /y
goto END

:END
echo Done.

And the post build event in say the winforms project would be:

call "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)\XML\myfile.xml"
"$(ProjectDir)..\WebApp\XML\myfile.xml"

This way, you only need to edit just one xml file in the winforms project and the other gets updated automatically. Then the XML files can be accessed with the paths provided by dacris, combined with relative folder XML.

Or if the web app is running on same computer as the client winforms app, use can you a common data folder:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM