簡體   English   中英

當我試圖進入相對路徑時,我得到的錯誤開始索引不能小於零

[英]When im trying to getting into a relative path im getting an error startindex cannot be less than zero

當我試圖進入字符串相對路徑時,我得到的錯誤startindex不能小於零,出現錯誤的原因是什么?

  string relativePath = Directory.GetCurrentDirectory();
  relativePath.Remove(relativePath.IndexOf(@"\GameSystem"));

當indexOf找不到字符串時,它返回-1。 如果刪除索引為-1的內容,則會得到您正在談論的錯誤。 就是說,relativePath不包含字符串。 只需控制台出您認為在那里的字符串,或在該行放置調試點,以查看當前目錄的真正含義。

似乎您需要添加一些條件邏輯,因此您可以處理不包含所需路徑段的字符串。 您可能要嘗試:

string relativePath = System.IO.Directory.GetCurrentDirectory();
int position = relativePath.IndexOf(@"\GameSystem");
if (position > 0)
{
    relativePath.Remove(position);
}
else
{
    //handle condition rather than throw "start index cannot be less than zero"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM