如何从共享点列表项 C# 中删除第一个字符

How to remove the first character from a sharepoint list item C#

我有一行代码基本上搜索共享点列表,如果找到与 pdf 匹配的名称,则将其附加到共享点列表中的该项目。我的问题是我正在搜索一个整数,但我正在搜索的列表中的列在所有整数之前都有一个 'M'。

例如,我正在搜索序列号 88912,但它永远找不到,因为在列表中它是 M88912。我附上了下面的具体代码。

命名空间 SharepointAttach { public 静态 class 附件文件 {

    public static void process()
    {
        List<string> mailto = new List<string> { @"datareports@***.com" };
        string sourcepath = @"\SERV\DocumentManagement\ASMEscans\UMscans\";
        string archivepath = @"\SERV\DocumentManagement\ASMEscans\UMscans\archive\";
        string logfile = @"\SERV\DocumentManagement\log.txt";

        //SharePointUtil su = new SharePointUtil();
        DebugLogNotify.notify notify = new DebugLogNotify.notify();
        DebugLogNotify.Logger logger = new DebugLogNotify.Logger(logfile);
        int firstserial;
        ListItem theListItem;
        Debug.Print(sourcepath);
        foreach (string f in Directory.GetFiles(sourcepath, @"M*.pdf"))
        {
            //Debug.WriteLine(f);
            string fn = Path.GetFileNameWithoutExtension(f);
            bool result = int.TryParse(Regex.Match(fn, @"\d+", RegexOptions.IgnorePatternWhitespace).Value, out firstserial);
            theListItem = FindULogListItem(firstserial);
            if (theListItem == null)
            {
                Debug.WriteLine("No matching item found in list: " + f);
                logger.Log(DateTime.Today + "ISSUE: No matching item found in list: " + f);
                notify.SendEmail(mailto, fn + " not Attached", "No matching item found in list: " + f, f);
            }
            else
            {
                string newfn = SharePointUtil.GetUniqueAttachmentName(f);
                SharePointUtil.FindAndAttachFile(theListItem, f, newfn);
                System.IO.File.Move(f, archivepath + newfn);
                logger.Log(DateTime.Today + "Attached " + f + " to list item with starting serial " + theListItem["Title"].ToString());
            }
        }

    }

    private static ListItem FindULogListItem(int serial)
    {
        bool result;
        int endserial, startserial;
        ClientContext clientContext = new ClientContext("http://companyweb");
        List oList = clientContext.Web.Lists.GetByTitle("UM Log");
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View></View>";
        ListItemCollection collListItem = oList.GetItems(camlQuery);
        clientContext.Load(collListItem);
        clientContext.ExecuteQuery();
        foreach (ListItem oListItem in collListItem)
        {
            result = int.TryParse(oListItem["Title"].ToString(), out startserial);
            result = int.TryParse(oListItem["Ending_x0020_Serial"].ToString(), out endserial);
            if ((serial >= startserial) && (serial <= endserial))
                return oListItem;
        }
        return null;
    }


}

}

当您从文件中获取整数时,您将删除 M。为什么不把它加回来?

var filename = "M" + result;

或者更好的是,你为什么要把号码拿出来?

var filename = Path.GetFileNameWithoutExtension(f);

我可以通过在代码的下半部分添加以下内容来解决这个问题:

   private static ListItem FindUMLogListItem(int serial)
    {
        bool result;
        int endserial, startserial;
        ClientContext clientContext = new ClientContext("http://companyweb");
        List oList = clientContext.Web.Lists.GetByTitle("UM Log");
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View></View>";
        ListItemCollection collListItem = oList.GetItems(camlQuery);
        clientContext.Load(collListItem);
        clientContext.ExecuteQuery();
        foreach (ListItem oListItem in collListItem)
        {

            result = int.TryParse(Regex.Match(oListItem["Title"].ToString(), @"\d+", RegexOptions.IgnorePatternWhitespace).Value, out startserial);
            result = int.TryParse(Regex.Match(oListItem["Ending_x0020_Serial"].ToString(), @"\d+", RegexOptions.IgnorePatternWhitespace).Value, out endserial);
            if ((serial >= startserial) && (serial <= endserial))
                return oListItem;
        }
        return null;

通过添加 regex.match,我能够在将字符串转换为整数之前删除 "M"。谢谢!