在成功的Windows身份验证之后运行Excel文件
问题描述:
我正在尝试将一些代码放在一起,这些代码要求用户输入当前的Windows用户名和密码才能运行Excel文件。但是,我遇到了我的代码部分process.start问题。不知道我错过了什么。在成功的Windows身份验证之后运行Excel文件
代码:
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身份验证\ C#\ TestApp \ Form1.cs
错误CS1513}预计TestApp C:\ C#\ Windows Authe ntication \ 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语句。
请添加确切的错误消息/例外,你正在。同时请说明您的实际代码是否编译,并确保您在问题中发布的[MCVE]反映了您的实际问题。到目前为止,代码只是语法错误。 –
附注:在SO上创建帖子时,请不要添加“谢谢”笔记,您的生活故事(新的/新的语言)以及标题中的标签。 –
@AlexeiLevenkov - 最重要的是,我觉得在预先赞赏的情况下是合适的。事实上,对于你的陈述的前一部分:在我以前的职位之一,我因为提供错误消息而受到责备,所以......我有点困惑。尽管坦率地说,我尊重我的代码在“语法上不正确”的事实。这就是我寻求帮助的原因。无论如何,谢谢你的建设性批评。有一个难忘的一天 –