We often encounter Path Problems during development. This involves two concepts: absolute path and relative path.
In winform development, we often use "Application. startupPath "to obtain the path of the currently running program, and then concatenate the name of the program to be run to run the program (of course, the name of the directly running program can also be 〕. This is the relative path and absolute path involved in the Application. We use "Application. startupPath "+" program name "is the absolute path, while directly using" program name "is the relative path (relative to the current running directory), although the effect is the same. However, there are differences in nature.
Relative paths are used in web development, and absolute paths are rarely used. It is used only when file operations are involved and other resources on the server are accessed. Typically, after a file is uploaded, the access to the file is Server. MapPath ("relative path parameter ").
A problem encountered a few days ago was that a c dll was used to implement specific functions. However, the encapsulated function requires two parameters and two path parameters. It is also a parameter that requires relative paths. Depressed, relatively easy to switch. Vs also provides direct functions. However, absolute conversion is not found. After googel, it was found that window apis were used in c language. So use c # to implement it and add the code.
[DllImport ("shlwapi. dll ", CharSet = CharSet. auto)] public static extern bool PathRelativePathTo ([Out] StringBuilder pszPath, string pszFrom, FileAttributes dwAttrFrom, string pszTo, FileAttributes dwAttrTo ); /// <summary> /// convert the absolute path to the relative path /// </summary> /// <param name = "absolutePath"> </param> /// <returns> </returns> public static string Transform (string absolutePath) {StringBuilder path = new StringBuilder (260); Commons. pathRelativePathTo (path, System. windows. forms. application. startupPath, System. IO. fileAttributes. directory, absolutePath, System. IO. fileAttributes. normal); return path. toString ();
}