Step 6 of. net development-absolute path conversion relative path

Source: Internet
Author: User

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 ();
        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.