c# - 在 Windows 身份验证成功后运行 Excel 文件

标签 c# visual-studio if-statement process.start

我试图将一些代码放在一起,要求用户输入他们当前的 Windows 用户名和密码,以便运行 excel 文件。但是,我在处理代码的 process.start 部分时遇到问题。不知道我错过了什么。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestApp
{
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("advapi32.dll")]
        public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool issuccess = false;
            string username = GetloggedinUserName();

            if (username.ToLowerInvariant().Contains(txtUserName.Text.Trim().ToLowerInvariant()) && username.ToLowerInvariant().Contains(txtDomain.Text.Trim().ToLowerInvariant()))
            {
                issuccess = IsValidateCredentials(txtUserName.Text.Trim(), txtPwd.Text.Trim(), txtDomain.Text.Trim());
            }

            if (issuccess)
                MessageBox.Show("Successfuly Login !!!");

// Here is where I am having trouble. I cannot get this file to run. 

                Process.Start("Z:\\FolderA\\Test.xlsx");


            else
                MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
        }

        private string GetloggedinUserName()
        {
            System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
            return currentUser.Name;
        }

        private bool IsValidateCredentials(string userName, string password, string domain)
        {
            IntPtr tokenHandler = IntPtr.Zero;
            bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
            return isValid;
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
  • Visual Studio 错误消息:

Severity Code Description
Error CS0103 The name 'Process' does not exist in the current context TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs
Error CS1513 } expected TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs

最佳答案

您的 IF 语句中缺少大括号:

        if (issuccess)
        {
            MessageBox.Show("Successfuly Login !!!");
            Process.Start("Z:\\FolderA\\Test.xlsx");
        }
        else
        {
            MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
        }

在多行 IF 语句中始终使用大括号是一条安全的规则。

关于c# - 在 Windows 身份验证成功后运行 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42171594/

相关文章:

c# - 在 Visual Studio 停止时使用react

visual-studio - 在未安装 VS 的情况下在构建服务器上构建 VS 2015 扩展?

c# - 有什么方法可以使用 Visual Studio 和 PTVS 调试 C# 中嵌入的 Python 代码吗?

html - 在 HTML [REGEX, RUBY] 中从 <!--[if gte mso 9]> 移除到 <![endif]-->

php - 如何将 if/else if 转换为循环

c# - 如何创建具有不同资源文件的通用 ViewModel?

c# - 我可以在 EF Core 中使用带有外键的接口(interface)并使用 Fluent API 将其设置为外键吗?

c# - 如何编译包含 Access 数据库的 Visual C# 2008 程序?

c++ - 有没有办法使用 __declspec(align) 来声明不同类型的对象

javascript - 在条件语句中提升函数