Path Combine: Leading Slash in relative path

  • strict warning: Non-static method view::load() should not be called statically in /home/content/02/6751802/html/techpint/sites/all/modules/views/views.module on line 843.
  • strict warning: Declaration of views_plugin_display::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_display.inc on line 1877.
  • strict warning: Declaration of views_plugin_display_page::options_submit() should be compatible with views_plugin_display::options_submit(&$form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_display_page.inc on line 481.
  • strict warning: Declaration of views_plugin_display_block::options_submit() should be compatible with views_plugin_display::options_submit(&$form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_display_block.inc on line 193.
  • strict warning: Declaration of views_plugin_display_attachment::options_submit() should be compatible with views_plugin_display::options_submit(&$form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_display_attachment.inc on line 228.
  • strict warning: Declaration of views_handler_field_broken::ui_name() should be compatible with views_handler::ui_name($short = false) in /home/content/02/6751802/html/techpint/sites/all/modules/views/handlers/views_handler_field.inc on line 641.
  • strict warning: Declaration of views_handler_sort_broken::ui_name() should be compatible with views_handler::ui_name($short = false) in /home/content/02/6751802/html/techpint/sites/all/modules/views/handlers/views_handler_sort.inc on line 82.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/handlers/views_handler_filter.inc on line 585.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/handlers/views_handler_filter.inc on line 585.
  • strict warning: Declaration of views_handler_filter_broken::ui_name() should be compatible with views_handler::ui_name($short = false) in /home/content/02/6751802/html/techpint/sites/all/modules/views/handlers/views_handler_filter.inc on line 609.
  • strict warning: Declaration of views_handler_filter_user_name::value_submit() should be compatible with views_handler_filter_in_operator::value_submit($form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/user/views_handler_filter_user_name.inc on line 144.
  • strict warning: Declaration of views_handler_filter_user_name::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/user/views_handler_filter_user_name.inc on line 144.
  • strict warning: Non-static method view::load() should not be called statically in /home/content/02/6751802/html/techpint/sites/all/modules/views/views.module on line 843.
  • strict warning: Declaration of views_handler_field_comment_username::init() should be compatible with views_handler_field::init(&$view, $options) in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 48.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 128.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_style_default.inc on line 25.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/content/02/6751802/html/techpint/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • strict warning: Non-static method view::load() should not be called statically in /home/content/02/6751802/html/techpint/sites/all/modules/views/views.module on line 843.
  • strict warning: Non-static method view::load() should not be called statically in /home/content/02/6751802/html/techpint/sites/all/modules/views/views.module on line 843.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.
  • warning: Creating default object from empty value in /home/content/02/6751802/html/techpint/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 33.

System.IO.Path.Combine is one of the scarcely used method provided by .Net Framework. It is used to combine two strings in to a complete path. Path.Combine is also essential for cross-platform coding as it uses whatever path separator the current OS uses.

 

string basePath = @"c:\";
string relativePath1 = "MyDocuments";
string relativePath2 = "hello.txt";
string combinedPath1 = basePath + relativePath1 + "\\" + relativePath2;    //Traditional Way
string combinedPath2 = System.IO.Path.Combine(basePath, relativePath1, relativePath2);
string combinedPath3 = System.IO.Path.Combine(basePath,"\\", relativePath1, relativePath2);

 

But take care of leading slash in relative path, if you add a leading slash in relative path Path.Combine will drop the basePath. You can add a trailing slash in first parameter i.e. string basePath = @"c:\";  

 

Output of the above statements is

combinedPath1 : c:\MyDocuments\hello.txt;
combinedPath2 : c:\MyDocuments\hello.txt;
combinedPath3 : \MyDocuments\hello.txt; //first parameter i.e basePath has been dropped.

Anonymous's picture

Good post

 What a wonderful guide for me? For a longtime, I have been searched for this but now only I got your blog through Google. Thanks for sharing. Keep on posting...

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr><br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong><font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <csharp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <sql>, <tsql>, <vbnet>, <xml>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Lines and paragraphs break automatically.

More information about formatting options

Who's online

There are currently 0 users and 6 guests online.

Recent Answers

Anonymous added a answer to 301 Redirect in asp.net (17 weeks 5 days ago)
 
Anonymous added a answer to 301 Redirect in asp.net (17 weeks 5 days ago)
 
Anonymous added a answer to 301 Redirect in asp.net (17 weeks 6 days ago)
 
Anonymous added a answer to 301 Redirect in asp.net (18 weeks 4 hours ago)
 
Anonymous added a answer to 301 Redirect in asp.net (18 weeks 1 day ago)
 
Anonymous added a answer to 301 Redirect in asp.net (18 weeks 1 day ago)