C#问题运行WPF

时间:2011-02-15 01:35:04

标签: c# visual-studio-2010 code-access-security securityexception

使用Visual Studio 2010在我的WPF应用程序中遇到一些问题,使用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。

2 个答案:

答案 0 :(得分:2)

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

[edit]你有权阅读该文件吗?奇。绝对尝试下面的内容,你不会确切知道发生了什么,直到你从被抛出的异常中获得更多细节。[/ 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并阅读错误消息时,将鼠标悬停在变量{{1}}上,应该为您提供有关正在发生的事情的更多信息。希望这有帮助!

答案 1 :(得分:0)

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

尝试以管理员身份运行visual studio(右键单击它的图标,然后选择“以管理员身份运行”)并查看是否有帮助。

或者,您可以在Windows资源管理器中选择该文件夹,并授予“用户”组阅读权限。 Here's a place to start