【问题标题】:Running an Excel File following Successful Windows Authentication在成功的 Windows 身份验证后运行 Excel 文件
【发布时间】:2017-02-11 02:40:12
【问题描述】:

我正在尝试整理一些代码,这些代码要求用户输入他们当前的 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 错误消息:

严重性代码说明
错误 CS0103 当前上下文 TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs 中不存在名称“进程”
错误 CS1513 } 预期 TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs

【问题讨论】:

  • 请准确添加您收到的错误消息/异常。另外请澄清您的实际代码是否编译,并确保您在问题中发布的minimal reproducible example 反映了您的实际问题。到目前为止,代码只是语法不正确。
  • 旁注:在 SO 上创建帖子时,请不要在标题中添加“谢谢”备注、您的生活故事(此处为新/语言新)和标签。
  • @AlexeiLevenkov - 最重要的是,我发现适当地表达预先的欣赏。顺便说一句,对于您声明的前一部分:我在之前的一篇文章中因提供错误消息而受到指责,所以......我有点困惑。坦率地说,但尊重,我知道我的代码是“语法不正确”的事实。这就是我寻求帮助的原因。无论如何,感谢您的建设性批评。度过美好的一天
  • 旁注:如果我删除进程启动,它将编译。 ——
  • 请使用System.Diagnostics.Process.Start("");查看

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


【解决方案1】:

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 语句中始终使用大括号是一条安全规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    相关资源
    最近更新 更多