C# 运行 WPF 时出现问题

发布于 2024-10-17 13:16:57 字数 790 浏览 2 评论 0原文

我的 WPF 应用程序使用 Visual Studio 2010(用 C# 构建)时遇到一些问题。目前出现的错误是:

SecurityException 未被用户代码处理

以下是当我单击按钮时的代码,它检查文本文件的大小,以及是否有音量,并为名为“ButtonToday”的按钮的背景着色。

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Gets current date and puts it into string.
    string today = DateTime.Now.ToString("yyyy.MM.dd");
    string yesterday = DateTime.Now.AddDays(-1).ToString("yyyy.MM.dd");

    TextBoxToday.Text = "" + today;
    TextBoxYesterday.Text = "" + yesterday;
    FileInfo f = new FileInfo("D:\\Client1\\2011.02.14.log");
    {
        if (f.Length > 0)
            ButtonToday.Background = Brushes.Green;
        else
            ButtonToday.Background = Brushes.Red;
    }
}

感谢您的任何帮助。我是一个n00b。

Having some problems in my WPF application using Visual Studio 2010, building in C#. The error coming up at the moment is:

SecurityException was unhandled by user code

The following is the code when I click a button, it checks the size of a text file, and if it has volume or not colors the background of a button called 'ButtonToday'.

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Gets current date and puts it into string.
    string today = DateTime.Now.ToString("yyyy.MM.dd");
    string yesterday = DateTime.Now.AddDays(-1).ToString("yyyy.MM.dd");

    TextBoxToday.Text = "" + today;
    TextBoxYesterday.Text = "" + yesterday;
    FileInfo f = new FileInfo("D:\\Client1\\2011.02.14.log");
    {
        if (f.Length > 0)
            ButtonToday.Background = Brushes.Green;
        else
            ButtonToday.Background = Brushes.Red;
    }
}

Thanks for any help. I am a n00b.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-10-24 13:16:57

您(或您的应用程序)似乎没有打开该文件的适当权限。检查并确保您可以通过文件系统自己访问该文件,听起来您可能不能。

[编辑]那么你确实有读取该文件的权限吗?奇怪的。一定要尝试下面的方法,除非您从抛出的异常中获得更多详细信息,否则您不会确切地知道发生了什么。[/edit]

尝试这样做:

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Gets current date and puts it into string.
    string today = DateTime.Now.ToString("yyyy.MM.dd");
    string yesterday = DateTime.Now.AddDays(-1).ToString("yyyy.MM.dd");
    TextBoxToday.Text = "" + today;
    TextBoxYesterday.Text = "" + yesterday;

     try
     {
         FileInfo f = new FileInfo("D:\\Client1\\2011.02.14.log");
         {
             if (f.Length > 0)
                 ButtonToday.Background = Brushes.Green;
             else
                 ButtonToday.Background = Brushes.Red;
         }
     }
     catch ( SecurityException ex )
     {
         ex.Message;
     }
}

ex.Message; 行上放置一个断点,然后在调试模式下运行您的程序。当您到达变量 ex 并阅读错误消息时,将鼠标悬停在变量上,应该会为您提供有关正在发生的情况的更多信息。希望这有帮助!

It seems you (or your application) doesn't have the proper permissions open the file. Check and make sure that you can access the file yourself through the filesystem, sounds like you probably can't.

[edit]You do have permission to read the file, then? Odd. Definitely try the below then, you won't know exactly what's going on until you get more detail from the exception being thrown.[/edit]

Try this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Gets current date and puts it into string.
    string today = DateTime.Now.ToString("yyyy.MM.dd");
    string yesterday = DateTime.Now.AddDays(-1).ToString("yyyy.MM.dd");
    TextBoxToday.Text = "" + today;
    TextBoxYesterday.Text = "" + yesterday;

     try
     {
         FileInfo f = new FileInfo("D:\\Client1\\2011.02.14.log");
         {
             if (f.Length > 0)
                 ButtonToday.Background = Brushes.Green;
             else
                 ButtonToday.Background = Brushes.Red;
         }
     }
     catch ( SecurityException ex )
     {
         ex.Message;
     }
}

Place a breakpoint over the ex.Message; line, then run your program in debug mode. Hover over the variable ex when you get to it and read the error messages, should give you more information as to what is going on. Hope this helps!

流绪微梦 2024-10-24 13:16:57

注意:如果您可以通过 Windows 资源管理器看到该文件,则可能是管理权限问题。

尝试以管理员身份运行 Visual Studio(右键单击其图标,然后选择“以管理员身份运行”),看看是否有帮助。

或者,您可以在 Windows 资源管理器中选择该文件夹,并授予“用户”组读取该文件夹的权限。 从这里开始

Note: If you can see the file through windows explorer, it might be an Administrative permissions issue.

Try running visual studio as an administrator (right-click on it's icon, and pick "Run as Administrator") and see if that helps.

Alternatively you could select the folder in windows explorer and give the "Users" group permission to read it. Here's a place to start

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文