Set the file path to the relative path in the Visual Studio Project

Source: Internet
Author: User

Introduction

In daily program development, many Visual Studio programs may need to add files other than the original program files, such as text files, xml files, and database files.

But usually directly copy these additional files to the project, the file path is absolute (the file is usually a fixed path on your computer, once the source file path is moved, the project cannot find the location of the file ).

Therefore, it is especially important to set the relative path of the file. this will help you package and issue your project (in this way, the file will be permanently bound to the project, so you don't have to worry about the program cannot find the file due to the deletion of the source file, as long as the files in the project are not deleted, the files will always exist ).

 

For example

Here, I directly paste the English dictionary TXT text file to the project file.

D: \ study \ cnblog \ blog log \ English-Chinese dictionary .txt

The path here is the absolute path, once I put D: \ study \ cnblog \ blog log \ English-Chinese dictionary \ the English-Chinese dictionary. TXT text file cannot be found after it is moved to another file directory.

Similarly, if I package the program project to someone else, someone else will not be able to run it on his computer after receiving it, because he does not have the text file on his computer, this file only exists on my computer.

D: \ study \ cnblog \ blog log \ English-Chinese dictionary \ directory.

The call path in the Code is as follows:

1 // read each line of data in English dictionary. txt
2 string [] strArr = File. readAllLines (@ "D: \ study \ cnblog \ blog log \ English-Chinese dictionary .txt", Encoding. default );

 

Therefore, if we want to flexibly use this text file, we need to change its absolute path to a relative path so that no matter whether I "D :\... how to move text files in the \ English-Chinese dictionary \ "directory does not affect the project program's calling of text files.

 

The following describes how to set the file path to relative path in a Visual Studio project.

First, copy and paste the English dictionary .txt to the project file. 01

Then, click "show all files". We can see that the hidden bin \ Debug file path contains four files, but there is no English dictionary. txt file. 02.

Copy and paste the English dictionary. txt file to the bin \ Debug directory. 03

Then, we can remove the BIND file from the project file. 04

Click "show all files" again to hide the bin and obj files. 05

In this example, the English-Chinese dictionary. txt has been stored in the project. You can copy and paste the text file directly under the project (under the absolute path) and delete it. 06

For this reason, the English dictionary .txt already exists in the relative paths of the Project. 07 and 08.

01 initial status 02 Click Show All files button 03 copy the text to the Debug directory

 

04 right-click binfile from project to exclude 05 Click Show All files again 06 at this time, the text can be deleted

 

07. txt text is stored in the Debug directory. 08, so the text is permanently stored in this project.

In this way, the English dictionary .txt is saved to the current project (in the bin \ Degug directory). You can call the relative path in the program.

1 // read the relative path of English dictionary. txt
2 string [] strArr = File. ReadAllLines (@ "文.txt .txt", Encoding. Default );
Related Article

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.