C# 更改文件夹位置时出错

C# 更改文件夹位置时出错,c#,string,linq,substring,C#,String,Linq,Substring,当我将我的位置更改为网络位置时,我收到一个错误。这只发生在一个文件夹中。我的错误是索引和长度必须引用子字符串中的位置 错误: 代码段: private void list() { List<string> stFileNames = new List<string>(); stFileNames.AddRange(arrRelease); foreach (var r in arrDraft) {

当我将我的位置更改为网络位置时,我收到一个错误。这只发生在一个文件夹中。我的错误是索引和长度必须引用子字符串中的位置

错误:

代码段:

private void list()
 {
 List<string> stFileNames = new List<string>();
        stFileNames.AddRange(arrRelease);
        foreach (var r in arrDraft)
        {
            if (stFileNames.FindAll(m => Path.GetFileNameWithoutExtension(m).ToUpper().Substring(0, 8).Equals(Path.GetFileNameWithoutExtension(r).ToUpper().Substring(0, 8))).Count == 0)
       //getting error in the above line.. Only when i am giving to one particular location
        /which i need then that time i am getting this error.
                stFileNames.Add(r);
        }

        dt.Columns.Add("Drawing Number");
        dt.Columns.Add("Drawing Path");
        dt.Columns.Add("Draft Path");
        dt.Columns.Add("Release Path");
        dt.Columns.Add("Error");
        dt.Columns.Add("Archive");

        List<FileDetails> lst = new List<FileDetails>();
        //matching files according to the realse folder
        foreach (string f in stFileNames)
        {
            Finder finder = new Finder(Path.GetFileName(f).Substring(0, 8));
            string abc = Array.Find(arrDraft, finder.Match);
            string def = Array.Find(arrRelease, finder.Match);
            string cdf = Array.Find(arrDrawing, finder.Match);
            //matching file in the location Drawing
            string ghi = Array.Find(arrArchive, finder.Match);
            //matching file in the location Archieve
            dt.Rows.Add(Path.GetFileNameWithoutExtension(f), cdf, abc, def, String.Empty, ghi);
        }
        dataGridView1.DataSource = dt;
    }
private void list()
{
List stFileNames=新列表();
stFileNames.AddRange(arrelease);
foreach(ARR草案中的var r)
{
if(stFileNames.FindAll(m=>Path.GetFileNameWithoutExtension(m).ToUpper().Substring(0,8).等于(Path.GetFileNameWithoutExtension(r.ToUpper().Substring(0,8)))。计数==0)
//在上述行中获取错误..仅当我提供给一个特定位置时
/这是我需要的,那时我得到了这个错误。
stFileNames.Add(r);
}
dt.列。添加(“图纸编号”);
dt.Columns.Add(“绘图路径”);
dt.Columns.Add(“草稿路径”);
dt.Columns.Add(“发布路径”);
dt.Columns.Add(“错误”);
dt.列。添加(“存档”);
List lst=新列表();
//根据realse文件夹匹配文件
foreach(stfilename中的字符串f)
{
Finder=newfinder(Path.GetFileName(f).Substring(0,8));
字符串abc=Array.Find(arrdaft,finder.Match);
字符串def=Array.Find(arrelease,finder.Match);
字符串cdf=Array.Find(arrDrawing,finder.Match);
//位置图形中的匹配文件
字符串ghi=Array.Find(arrArchive,finder.Match);
//在位置存档中匹配文件
添加(Path.GetFileNameWithoutExtension(f)、cdf、abc、def、String.Empty、ghi);
}
dataGridView1.DataSource=dt;
}

我假设您使用的路径中的文件名少于8个字符,因此对子字符串(0,8)的调用失败

在调用子字符串之前,需要检查文件名的长度。假设对于8个字符以下的文件名,您只需要整个文件名,您可以执行以下操作:

var charactersToRead = Path.GetFileNameWithoutExtension(m).length < 8 ? Path.GetFileNameWithoutExtension(m).length : 8
var charactersToRead=Path.GetFileNameWithoutExtension(m).长度<8?Path.GetFileNameWithoutExtension(m)。长度:8

然后沿着
Substring(0,charactersToRead)

@alykins
Path.GetFileNameWithoutExtension(m).ToUpper().Substring(0,8).Equals(Path.GetFileNameWithoutExtension(r.ToUpper().Substring(0,8)).Count==0)的行更改方法调用。
您的文件名长度不是8个字符,正在抛出该错误。是的,很抱歉Stacy,在我发布了那个问题后,我在代码中看到了你的评论-现在看到了。在那一行上打断,看看列表中的成员-其中一个或多个长度将少于8个字符。您可能可以将其修改为类似于
ithoutExtension(m).Where(m.length>7.ToUpper()
,但该语法是错误的-我不知道正确的语法是什么;其他人可能知道better@alykins
错误1'string'不包含对'Where'的定义,并且找不到扩展方法'Where'接受'string'类型的第一个参数(是否缺少using指令或程序集引用?
是,我在这方面的语法太离谱了——我对LINQ充其量是危险的——这是值得研究的东西(我想无论如何),这里有一些真正精巧的LINQ查询编写者。你将不得不玩一会儿,然后可能会找人来帮助解决问题。更有用的IMO-+1