C# 变量不显示值

C# 变量不显示值,c#,C#,我正在运行下面的代码,如果我一步一步地通过代码,它会显示我的值,但当它到达 if (Directory.Exists(folderPath)) folderPath中未显示fName。它只显示例如 "\\\\Delta\\" + fName + "_Monday\\" 有人能告诉我应该更新什么才能让它正常工作吗 public static void OpenExcelWorkbook() { fName = new string[4] { "Mike", "Joe", "Hickne

我正在运行下面的代码,如果我一步一步地通过代码,它会显示我的值,但当它到达

if (Directory.Exists(folderPath))
folderPath中未显示fName。它只显示例如

"\\\\Delta\\" + fName + "_Monday\\"
有人能告诉我应该更新什么才能让它正常工作吗

public static void OpenExcelWorkbook()
{
    fName = new string[4] { "Mike", "Joe", "Hickney", "Rich", };
    folderPaths = new string[7] 
    {
        "\\\\Delta\\" + fName + "_Monday\\",
        "\\\\Delta\\" + fName + "_Tuesday\\",
        "\\\\Delta\\" + fName + "_Wednesday\\",
        "\\\\Delta\\" + fName + "_Thursday\\",
        "\\\\Delta\\" + fName + "_Friday\\",
        "\\\\Delta\\" + fName + "_Saturday\\",
        "\\\\Delta\\" + fName + "_Sunday\\",
    };
    fileNames = new string[4]
    {
        fName + "_generaldaily_file.xlsx",
        fName + "_employeeDaily_cumulative.xls",
        fName + "_generaldaily_file.xlsx",
        fName + "_employeeDaily_cumulative.xls",
        };

    Excel.Workbook wb = null;
    try
    {
        for (int q = fName.GetLowerBound(0); q <= fName.GetUpperBound(0); q++)
        {
            foreach (string fileName in fileNames)
            {
                foreach (string folderPath in folderPaths)
                {
                    if (Directory.Exists(folderPath))
                    {
                        foreach (string filePath in Directory.GetFiles(folderPath))
                        {
                            string temp = Path.GetFileName(filePath).ToLower();
                            if (temp == fileName.ToLower())
                            {
                                oWB = (Excel._Workbook)(oXL.Workbooks.Open(folderPath + fileName));
                                oWB = oXL.ActiveWorkbook;
                                oWB.RefreshAll();
                                //Calling method to save workbook here
                            }
                        }
                    }
                }
            }
        }
    }
    catch { }
}
publicstaticvoidopenexcelworkbook()
{
fName=新字符串[4]{“Mike”、“Joe”、“Hickney”、“Rich”,};
FolderPath=新字符串[7]
{
“\\\\Delta\\”+fName+“\\\\u星期一\\”,
“\\\\Delta\\”+fName+“\\\\u星期二\\”,
“\\\\Delta\\”+fName+“\\\\u周三\\”,
“\\\\Delta\\”+fName+“\\\\u星期四\\”,
“\\\\Delta\\”+fName+“\\\\u星期五\\”,
“\\\\Delta\\”+fName+“\\\\u星期六\\”,
“\\\\Delta\\”+fName+“\\\\u Sunday\\”,
};
fileNames=新字符串[4]
{
fName+“_generaldaily_file.xlsx”,
fName+“\u employeeDaily\u cumulative.xls”,
fName+“_generaldaily_file.xlsx”,
fName+“\u employeeDaily\u cumulative.xls”,
};
Excel.Workbook wb=null;
尝试
{

对于(int q=fName.GetLowerBound(0);q我会将代码重构为以下内容,因为我不知道如何将值赋给fName,如果显式传递该参数会更好

public static void OpenExcelWorkbook(string fName)
    {
        public string[] GetFolderPaths() 
        {
            return new string[7] = {
            string.format("\\\\Delta\\{0}_Monday\\",fName),
            string.format("\\\\Delta\\{0}_Tuesday\\",fName),
            string.format("\\\\Delta\\{0}_Wednesday\\",fName),
            string.format("\\\\Delta\\{0}_Thursday\\",fName),
            string.format("\\\\Delta\\{0}_Friday\\",fName),
            string.format("\\\\Delta\\{0}_Saturday\\",fName),
            string.format("\\\\Delta\\{0}_Sunday\\",fName)
            }
        };
       // the same applies to the filenames
        public string[] GetFileNames(){
            return new string[4] ={
            string.format("{0}_generaldaily_file.xlsx",fName),
            string.format("{0}_employeeDaily_cumulative.xls",fName),
            string.format("{0}_generaldaily_file.xlsx",fName),
            string.format("{0}_employeeDaily_cumulative.xls",fName)}

        }

        foreach (string fileName in GetFileNames())
        {
            foreach (string folderPath in GetFolderPaths())
            {
              // do your logic here
            }
        }
      }

建议使用Path.Combine而不是手动连接目录名,最好对字符串文本使用
@“
,这样可以避免转义反斜杠。例如

Path.Combine(@"\\Delta", fName + "_Monday");
您不需要将路径名和文件名列表预先生成为数组,只需在遍历名称列表时按需创建它们即可

string root = @"\\Delta";

String[] dayOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
String[] fileNames = { "generaldaily_file.xlsx", "employeeDaily_cumulative.xls", "generaldaily_file.xlsx", "employeeDaily_cumulative.xls" };
String[] fName = { "Mike", "Joe", "Hickney", "Rich" };

foreach (string name in fName) {
    foreach (string day in dayOfWeek)
        var folderPath = Path.Combine(root, name + "_" + day);
        if (Directory.Exists(folderPath)) {
            foreach (string filePath in Directory.GetFiles(folderPath) {
                string temp = Path.GetFileName(filePath);
                foreach (string fileName in fileNames) {
                    if (temp.toLower() == (name + "_" + fileName).toLower()) {
                        ...
                    }
                }
            }
        }
    }
}

另外,不要用空的catch块吞掉异常,否则如果出现问题,您将不知道是什么问题。至少您应该打印或记录堆栈跟踪,以便确定问题可能在哪里。

fname声明在哪里?我在这里看不到代码。我会将其添加进来,请给我一分钟的时间,它只显示用于检查ple.“\\\\Delta\\\”+fName+“\u Monday\\\”
你的意思是什么?任何字符串如何显示?你正在向字符串添加一个数组…你到底希望发生什么?@darkpbj他正在定义一个字符串数组(folderpath)在初始化中使用fname。它将不起作用。+1用于按需创建。但我不认为Path.Combine是必要的。