如何从登录名获取用户名以过滤文件 [英] How to get username from login to filter files

查看:60
本文介绍了如何从登录名获取用户名以过滤文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是这个人的新手,我坚持这样做.我需要在会话中获取用户名,以用于过滤我读取的文件的名称.

源代码

Hi guys,

I am new to this one and I am stuck doing it. I need to get the username in the session, to be used to filter the names of the files I read.

Source Code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Timers;
using System.Threading;
using System.IO;
using System.Net;

namespace BLM_USDD_TESTING
{
    public partial class Batch : System.Web.UI.Page
    {




        public string FilePath;
        public bool IsCallBack { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

            lstFiles.DataSource = null;
            lstFiles.Items.Clear();



            if (cbReport.Checked == true)
            {

                string[] FileEntries = null;
                string FileName = null;
                //counters is the directory name where in the text files are resided.     
                FilePath = (Session["UploadReport"].ToString());
                try
                {
                    FileEntries = Directory.GetFiles(FilePath);
                }
                catch (Exception)
                {
                    Console.WriteLine("{0} Exception caught.");
                }


                if (Page.IsPostBack)
                {
                    foreach (string report in FileEntries)
                    {
                        FileName = Path.GetFileName(report);
                        this.lstFiles.Items.Add(FileName);

                    }

                }

                if (lstFiles.SelectedIndex != -1)
                    lstFiles.Items.RemoveAt(lstFiles.SelectedIndex);
                cbError.Checked = false;
                cbDone.Checked = false;




            }
            else if (cbDone.Checked == true)
            {
                cbReport.Checked = false;
                string[] FileEntries = null;
                string FileName = null;
                //counters is the directory name where in the text files are resided.     
                FilePath = (Session["UploadDone"].ToString());
                try
                {
                    FileEntries = Directory.GetFiles(FilePath);
                }
                catch (Exception)
                {
                    Console.WriteLine("{0} Exception caught.");
                }

                if (Page.IsPostBack)
                {
                    foreach (string zip in FileEntries)
                    {
                        FileName = Path.GetFileName(zip);
                        this.lstFiles.Items.Add(FileName);

                    }

                }

                if (lstFiles.SelectedIndex != -1)
                    lstFiles.Items.RemoveAt(lstFiles.SelectedIndex);

                cbError.Checked = false;
                string blankText = "";
                txtDisp.Text = blankText;

            }
            else if (cbError.Checked == true)
            {
                string[] FileEntries = null;
                string FileName = null;
                //counters is the directory name where in the text files are resided.     
                FilePath = (Session["UploadError"].ToString());
                try
                {
                    FileEntries = Directory.GetFiles(FilePath);
                }
                catch (Exception)
                {
                    Console.WriteLine("{0} Exception caught.");
                }

                if (Page.IsPostBack)
                {
                    foreach (string zip in FileEntries)
                    {
                        FileName = Path.GetFileName(zip);
                        this.lstFiles.Items.Add(FileName);

                    }

                }

                if (lstFiles.SelectedIndex != -1)
                    lstFiles.Items.RemoveAt(lstFiles.SelectedIndex);
                cbDone.Checked = false;
                cbReport.Checked = false;
                string blankText = "";
                txtDisp.Text = blankText;


            }


        }






        protected void btnLoad_Click1(object sender, EventArgs e)
        {
          

            //Timer1.Enabled = true;
            //lblStatus.Text = "processing,please wait";
            Session["UploadPath"] = System.Configuration.ConfigurationManager.AppSettings["UploadPath"];
            Session["UploadError"] = System.Configuration.ConfigurationManager.AppSettings["UploadError"];
            Session["UploadReport"] = System.Configuration.ConfigurationManager.AppSettings["UploadReport"];
            Session["UploadDone"] = System.Configuration.ConfigurationManager.AppSettings["UploadDone"];

     
            {

                if (FileUpload1.HasFile)
             
                    try
                    {
                        lblStatus.Text = "processing,please wait";
                        string[] parts = FileUpload1.FileName.Split(new char[] { '.' });
                        if (parts.Length != 4)
                        {
                            lblStatus.Text = "Invalid file name";
                            return;
                        }
                        int intPar;
                        if (!int.TryParse(parts[1], out intPar))
                        {
                            lblStatus.Text = "Invalid date format";
                            return;
                        }

                        Session["BatchFile"] = FileUpload1.FileName;
                        FileUpload1.SaveAs(Session["UploadPath"].ToString() + Session["BatchFile"].ToString());
                        this.txtDisp.Text = "File successfully uploaded";
                        /*this.txtDisp.Text = "File name: " +
                             FileUpload1.PostedFile.FileName + "<br>" +
                             FileUpload1.PostedFile.ContentLength + " kb<br>" +
                             "Content type: " +
                             FileUpload1.PostedFile.ContentType;*/
                    }
                    catch (Exception ex)
                    {
                        this.txtDisp.Text = "ERROR: " + ex.Message.ToString();
                    }
                else
                {
                    this.txtDisp.Text = "You have not specified a file.";
                    
                   
                }
            }
               }

推荐答案

在MVC中,它位于
In MVC it sits in
HttpContext.Current.Session["username"]


我只假定在WebForms中是相同的;)


I only assume that in WebForms it is the same ;)


这篇关于如何从登录名获取用户名以过滤文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆