意外的角色''

时间:2017-10-17 15:52:09

标签: visual-studio visual-studio-2017 msbuild-15

从VS2013迁移到VS2017后,我遇到意外的字符问题。

在msbuild 12.0和VS2013中一切都很好,但是当移动到15.0时,我会收到数百个:

  

CS1519令牌无效'?'在类,结构或接口成员声明中

在msbuild命令行中。

在VS2017内部构建返回:

  

CS1056意外的字符''

var businessRuleData = principal.GetBusinessRule(​
BusinessRuleEnum.CONTENT_REPOSITORY);

Ch66发生错误,该区域之间为(B。隐藏的字符在写字板中变为?。但是,如上所述,相同的代码在msbuild 12.0中构建良好。删除所有代码并重新下载表单TFS没有解决问题

解决方案代码

注意:在change_me的代码中搜索,并确保更改为您想要的项目。

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace FixZeroWidthSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            // change to your directory
            var files = Directory.GetFiles(@"D:\change_me", "*.cs", SearchOption.AllDirectories);
            var counter = 0;
            var counterEdited = 0;
            var totalFiles = files.Length;
            var failed = new List<string>();
            var found = new List<string>();
            TfsTeamProjectCollection tpc = null;
            Workspace ws = null;

            foreach (var file in files)
            {
                if(counter % 10 == 0)
                {
                    Console.WriteLine("Searched {0} or {1} files, {2} have been edited.", counter, totalFiles, counterEdited);
                }
                // change to any folders you want to ignore or remove if none
                if (!file.Contains("change_me_ignore_folder_name"))
                {
                    string text = File.ReadAllText(file);
                    var regex = new Regex("[\u200B-\u200D\uFEFF]");
                    var newText = regex.Replace(text, "");

                    if (text != newText)
                    {
                        try
                        {
                            if (ws == null || tpc == null)
                            {
                                // change to your TFS server
                                tpc = new TfsTeamProjectCollection(new Uri(@"http://change_me_your_tfs_url/tfs/DefaultCollection"));
                                ws = FindWorkspaceByPath(tpc, file);
                            }

                            FileAttributes attributes = File.GetAttributes(file);

                            if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                            {
                                attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
                                File.SetAttributes(file, attributes);
                            }

                            ws.PendEdit(file);

                            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            {
                                // Make the file RW
                                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
                                File.SetAttributes(file, attributes);
                            }

                            File.WriteAllText(file, newText);
                            found.Add(file);
                            counterEdited++;
                        }
                        catch(Exception ex)
                        {
                            failed.Add(file);
                        }
                    }
                }
                counter++;
            }

            tpc.Dispose();
            File.WriteAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\found.txt", found);
            File.WriteAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\failed.txt", failed);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
        {
            return attributes & ~attributesToRemove;
        }

        private static Workspace FindWorkspaceByPath(TfsTeamProjectCollection tfs, string workspacePath)
        {
            VersionControlServer versionControl = tfs.GetService<VersionControlServer>();
            WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath);

            if (workspaceInfo != null)
            {
                return versionControl.GetWorkspace(workspaceInfo);
            }

            // No Workspace found using method 1, try to query all workspaces the user has on this machine.
            Workspace[] workspaces = versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
            foreach (Workspace w in workspaces)
            {
                foreach (WorkingFolder f in w.Folders)
                {
                    if (f.LocalItem.Equals(workspacePath))
                    {
                        return w;
                    }
                }
            }
            if (workspaces.Length > 0)
            {
                return workspaces[0];
            }

            throw new Exception(String.Format("TFS Workspace cannot be determined for {0}.", workspacePath));
        }
    }
}

2 个答案:

答案 0 :(得分:0)

解决方案

删除所有无效字符,因为MSBuild 15.0和VS2017对这些unicode字符更严格。

可以使用以下代码来完成删除文件夹中存在问题的所有代码。我使用了这个,因为所需的更改太大而无法手工完成。

C#代码

变量

[插入要扫描的文件夹]:示例C:\ TFS \ Code \ Branch \ Folder
[插入要忽略的文件夹]:示例3rdPartyCode

代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace FixZeroWidthSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            var files = Directory.GetFiles(@"D:\TFS\210-219\212\MCET_212", "*.cs", SearchOption.AllDirectories);
            var counter = 0;
            var counterEdited = 0;
            var totalFiles = files.Length;
            var failed = new List<string>();
            var found = new List<string>();

            foreach (var file in files)
            {
                if(counter % 10 == 0)
                {
                    Console.WriteLine("Searched {0} or {1} files, {2} have been edited.", counter, totalFiles, counterEdited);
                }
                if (!file.Contains("[Insert Folder To Ignore]"))
                {
                    string text = File.ReadAllText(file);
                    var regex = new Regex("[\u200B-\u200D\uFEFF]");
                    var newText = regex.Replace(text, "");

                    if (text != newText)
                    {
                        try
                        {
                            FileAttributes attributes = File.GetAttributes(file);

                            if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                            {
                                attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
                                File.SetAttributes(file, attributes);
                            }

                            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            {
                                // Make the file RW
                                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
                                File.SetAttributes(file, attributes);
                            }

                            File.WriteAllText(file, newText);
                            found.Add(file);
                            counterEdited++;
                        }
                        catch(Exception ex)
                        {
                            failed.Add(file);
                        }
                    }
                }
                counter++;
            }

            File.WriteAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\found.txt", found);
            File.WriteAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\failed.txt", failed);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
        {
            return attributes & ~attributesToRemove;
        }
    }
}

C#代码w / TFS结帐

变量

[插入要扫描的文件夹]:示例C:\ TFS \ Code \ Branch \ Folder
[插入要忽略的文件夹]:示例3rdPartyCode
[将URI插入TFS服务器集合]:示例http://tfs.company.com:8080/tfs/DefaultCollection

代码

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace FixZeroWidthSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            var files = Directory.GetFiles(@"[Insert Folder to Scan]", "*.cs", SearchOption.AllDirectories);
            var counter = 0;
            var counterEdited = 0;
            var totalFiles = files.Length;
            var failed = new List<string>();
            var found = new List<string>();
            TfsTeamProjectCollection tpc = null;
            Workspace ws = null;



            foreach (var file in files)
            {
                if(counter % 10 == 0)
                {
                    Console.WriteLine("Searched {0} or {1} files, {2} have been edited.", counter, totalFiles, counterEdited);
                }
                if (!file.Contains("3rdparty"))
                {
                    string text = File.ReadAllText(file);
                    var regex = new Regex("[\u200B-\u200D\uFEFF]");
                    var newText = regex.Replace(text, "");

                    if (text != newText)
                    {
                        try
                        {
                            if (ws == null || tpc == null)
                            {
                                tpc = new TfsTeamProjectCollection(new Uri(@"[Insert URI to TFS Server Collection]"));
                                ws = FindWorkspaceByPath(tpc, file);
                            }

                            FileAttributes attributes = File.GetAttributes(file);

                            if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                            {
                                attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
                                File.SetAttributes(file, attributes);
                            }

                            ws.PendEdit(file);

                            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            {
                                // Make the file RW
                                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
                                File.SetAttributes(file, attributes);
                            }

                            File.WriteAllText(file, newText);
                            found.Add(file);
                            counterEdited++;
                        }
                        catch(Exception ex)
                        {
                            failed.Add(file);
                        }
                    }
                }
                counter++;
            }

            tpc.Dispose();
            File.WriteAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\found.txt", found);
            File.WriteAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\failed.txt", failed);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
        {
            return attributes & ~attributesToRemove;
        }

        private static Workspace FindWorkspaceByPath(TfsTeamProjectCollection tfs, string workspacePath)
        {
            VersionControlServer versionControl = tfs.GetService<VersionControlServer>();

            WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath);

            if (workspaceInfo != null)
            {
                return versionControl.GetWorkspace(workspaceInfo);
            }

            // No Workspace found using method 1, try to query all workspaces the user has on this machine.
            Workspace[] workspaces = versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
            foreach (Workspace w in workspaces)
            {
                foreach (WorkingFolder f in w.Folders)
                {
                    if (f.LocalItem.Equals(workspacePath))
                    {
                        return w;
                    }
                }
            }

            throw new Exception(String.Format("TFS Workspace cannot be determined for {0}.", workspacePath));
        }
    }
}

答案 1 :(得分:0)

我最近发现这是我的一种解决方案。我的代码中没有任何内容。

如果我清洁溶液(右键单击溶液->清洁溶液),问题就消失了。