日期:2014-05-17  浏览次数:20958 次

C#打开word只能用绝对路径吗。为什么不能用相对路径
如题。。

------解决方案--------------------
可以自己定义相对路径。
相对于何处自己定义。
比如相对于当前程序所在目录,可以用System.AppDomain.CurrentDomain.BaseDirectory得到当前目录。
然后加上你传入的文件名即可得到完整路径

例如:
当前文件在:C:\Projects\Demo\bin\Debug\
你传入test.doc
加起来就得到一个绝对路径了:C:\Projects\Demo\bin\Debug\test.doc


获得常用目录:
C# code

一个WinForm程序,项目文件存放于D:\Projects\Demo,编译后的文件位于D:\Projects\Demo\bin\Debug,最后的结果如下:

1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe
2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug
3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug
4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\
5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\
6、System.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug
7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE

System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\
System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\

------解决方案--------------------
C# code


string basicPath = System.AppDomain.CurrentDomain.BaseDirectory;
string wordPath = System.IO.Path.Combine(basicPath, "word.doc");