简体   繁体   English

C# - 使用 File.Exists,当文件不存在时不运行最后一个 else 语句

[英]C# - Using File.Exists, when file DOESN'T exist does not run the last else statement

Super new to C#. C# 超级新手。 I'm having an input get split and then find an ID from the pointsTarget var.我正在拆分输入,然后从 pointsTarget var 中找到一个 ID。 When the file DOES exist, it seems that the line "else if (File.Exists(filePath+userId+txt))" returns "true" because it runs just fine and sets the argument "addPointsCompleted" to TRUE.当文件确实存在时,“else if (File.Exists(filePath+userId+txt))”行似乎返回“true”,因为它运行得很好,并将参数“addPointsCompleted”设置为 TRUE。 It works just how I would expect.它就像我所期望的那样工作。 But when the file does NOT exist, I want it to return false and run the last else statement: CPH.SetArgument("missing", "True");但是当文件不存在时,我希望它返回 false 并运行最后一个 else 语句: CPH.SetArgument("missing", "True"); and set "missing" to TRUE.并将“缺失”设置为 TRUE。

I feel like there is something wrong with the way I put in the if/else if/else statement because I get an error "System.IO.FileNotFoundException: Could not find file 'E:\Users\Troy\Documents\Stuff\PointNames\Test.txt'.我觉得我在 if/else if/else 语句中的输入方式有问题,因为我收到错误“System.IO.FileNotFoundException:找不到文件 'E:\Users\Troy\Documents\Stuff\PointNames \Test.txt'。

using System;
using System.IO;

public class CPHInline
{
    public bool Execute()
    {
        string rawInput = args["rawInput"].ToString();
        string[] split= rawInput.Split('+');
        var pointsTarget = split[0].ToString();
        var addPoints = split[1].ToString();
        CPH.LogInfo($"pointsTarget is {pointsTarget}");
        CPH.LogInfo($"addPoints is {addPoints}");
        var user = args["user"].ToString();
        CPH.SetArgument("pointsTarget", pointsTarget);
        string userPath = @"E:/Users/Troy/Documents/Stuff/PointNames/";
        string filePath = @"E:/Users/Troy/Documents/Stuff/PointIDs/";
        string txt = ".txt";
        var userId = File.ReadAllText(userPath+pointsTarget+txt);
        CPH.LogInfo($"userId is {userId}");

        if (user == pointsTarget)
        {
            CPH.SetArgument("corrupt", "True");
        }
        else if (File.Exists(filePath+userId+txt))
        {
            //DO THIS
            string fileName = filePath+userId+txt;
            string points = File.ReadAllText(fileName);
            int x = Convert.ToInt32(points);
            int y = Convert.ToInt32(addPoints);
            int sum = x + y;
            String newPoints;
            newPoints = sum.ToString();
            File.WriteAllText(fileName, newPoints);
            CPH.SetArgument("newPoints", newPoints);
            CPH.SetArgument("addPointsCompleted", "True");
        }
            
        else
        {
            //do this
            CPH.SetArgument("missing", "True");
        }

        return true;

     
    }
}

I tried looking around, but all the issues are from people where the file DOES exist and they can't find it.我试着环顾四周,但所有问题都来自文件确实存在但他们找不到它的人。 My problem is kind of the opposite.我的问题恰恰相反。

I feel like there is something wrong with the way I put in the if/else if/else statement because I get an error "System.IO.FileNotFoundException: Could not find file 'E:\Users\Troy\Documents\Stuff\PointNames\Test.txt'.我觉得我在 if/else if/else 语句中的输入方式有问题,因为我收到错误“System.IO.FileNotFoundException:找不到文件 'E:\Users\Troy\Documents\Stuff\PointNames \Test.txt'。

This is a good opportunity for you to start familiarizing yourself with using a debugger to step through the code and observe its behavior.这是您开始熟悉使用调试器单步调试代码并观察其行为的好机会。 Because the problem has nothing to do with your if structure.因为问题与您的if结构无关。 It's happening before your if block.它发生你的if块之前。 Right here:就在这儿:

var userId = File.ReadAllText(userPath+pointsTarget+txt);

Look at the error message.查看错误消息。 It's trying to read a file in the "PointNames" folder.它正在尝试读取"PointNames"文件夹中的文件。 Which is in your userPath variable:在您的userPath变量中:

string userPath = @"E:/Users/Troy/Documents/Stuff/PointNames/";

Which is only ever used in that one line of code that tries to read a file.仅在尝试读取文件的那一行代码中使用过。 And File.ReadAllText will throw a FileNotFoundException if the file is not found.如果找不到文件, File.ReadAllText将抛出FileNotFoundException

It seems you're already aware of how to check if a file exists.您似乎已经知道如何检查文件是否存在。 So why not apply that here?那么为什么不在这里应用呢? For example:例如:

var userId = string.Empty;
if (File.Exists(userPath+pointsTarget+txt))
{
    userId = File.ReadAllText(userPath+pointsTarget+txt);
}
else
{
    // handle the error in some way
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM