C# error1 :The name 'DateTimeStyles' does not exist in the current context. error2:找不到类型或命名空间名称 'CultureInfo'

标签 c# .net

您好,我已尽最大努力在 Google 上进行搜索,但无法从这些错误中恢复:

名称“DateTimeStyles”在当前上下文中不存在

...

找不到类型或命名空间名称“CultureInfo”(是否缺少 using 指令或程序集引用?)

..如果我遗漏了什么,你能帮我解决一下吗..这是我的代码..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Security.Permissions;
using System.Threading;

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                   "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                   "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                   "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                   "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
            string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                        "5/1/2009 6:32:00", "05/01/2009 06:32", 
                        "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"};
            DateTime dateValue;

            foreach (string dateString in dateStrings)
            {
                if (DateTime.TryParseExact(dateString, formats,
                                           new CultureInfo("en-US"),
                                           DateTimeStyles.None,
                                           out dateValue))
                    Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
                else
                    Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
            }
        }
    }
}

我正在使用 visual studio 2005。

最佳答案

看起来您的 using 语句没问题。必须缺少引用。错误消息中提到的两种类型都存在于 mscorlib.dll 中。

我相信 C# 编译器总是引用 mscorlib,除非您使用 /nostdlib 命令行选项要求它不要这样做。

你如何编译你的程序?如果使用 Visual Studio,请检查项目设置中的选项“不使用 stdlib”或类似选项(另一个答案解释了在哪里可以找到该选项)。如果使用命令行编译器,请确保您没有传递 /nostdlib 选项。

关于C# error1 :The name 'DateTimeStyles' does not exist in the current context. error2:找不到类型或命名空间名称 'CultureInfo',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887667/

相关文章:

c# - move 无边界表单

c# - LocalDb 和 IdentityDbContext Entity Framework 错误 26

c# - Object.Equals(Object, Object) 处的 NullReferenceException

c# - LINQ- 组合多个 List<T> 并按值排序 (.Net 3.5)

c# - 添加了Web服务引用,导致缺少 namespace 的错误

c# - 使用带有域服务的 ado.net Entity Framework 更新父表

.net - 解决方案源代码组织指南(OO/DDD)

c# - 为什么我需要成为管理员才能读取 .NET CLR 内存性能计数器

c# - 如何在一段时间后强行关闭c#中的进程?

c# - Kubernetes - 找出服务中有多少个副本?