"Directory does not exist" but it does

Currently getting ArgumentException: Directory does not exist Parameter name: path for a path that does exist within the system.

  1. This is within a SystemFileWatcher creation.

  2. I am using System.IO and UnityEngine within the script

  3. The path in question does lead outside of the project files.

  4. The file does need to be located where it is.

  5. Path string is shorter than 256 characters

What could be the issue?

How can I fix this?

There can be a number of reasons

examples:

if you run the exe as another user - your personal mapped network drive is not available
If you run the exe as another user - it may not have permission to see it
if you run the exe as a server - remote drives arent available
Some network drives seem to be allergic to being accessed directly

so, context is key here

so I would need to move this off of anything OneDrive related surely. How would I go about making sure the project has perms for the file? All of this is internal memory, so nothing fancy or remote.

Oh lord, the most horrific thing you can do to unity is work on it on a onedrive area…
One thing to note, your one drive area, isnt always the path you think it is :smiley:

If you arent running as another user, it is 100% running as you and you can see it, it isnt a permissions thing… unless you run it as a service

it is running on system admin, so perms shouldn’t be the issue? it’s still giving the same exception even after I move the file location and changed the path to another drive entirely. so what sort of troubleshooting can I do here other than that? I’ve checked perms, I’ve moved it around, I double checked the path, and even sent my buddy a screenshot of the file which he confirmed did exist. I’m at a loss

best answer? show some code, and some debugging output.

Is the path a whole path or a partial, are you sure its running from where you think it is if it is partial

full path, just a txt doc so I can connect unity to streamerbot without having to spend a week learning websockets for both programs. there’s nothing to put a debug on:

  void Start()
    {
        FileSystemWatcher fileWheelInput = new FileSystemWatcher(@"path");


            fileWheelInput.NotifyFilter = NotifyFilters.LastWrite;

            fileWheelInput.Changed += OnChanged;

    }

pretty fresh into C# and Unity, so everything’s pretty quick and dirty at the moment.

I’m using OnChanged to set a bool to true in order to trigger an if statement in Update which triggers an action in unity.

ok so this says where ever i made the home of running i expect a folder called path.

so if you ran a build it would be like c:\users\yourname\documents\unity\project\build\path … but if you run it during dev time it might be only as far as project but then assets\path … so…

so for it to work within editor I need to store the file within assets?

well thats kinda my point - where does it think its running from? what is its current directory

the script is placed on an object via component, so wouldn’t that just mean the asset/scripts dir?

or are you asking where the FileSystemWatcher is pointed?

because that’s all the way out to C:/users/name/docs/streamerbot/redeemfiles/file.txt

NEVER place a Unity project under a cloud drive folder! Catastrophic data loss is almost guaranteed if you work from more than one place, more so if others work in the same project. And if you use it only for backup reasons, your backup will easily be bloated by a factor of 10 or more.

Use source control! Cloud drives do not warn about conflicts, nor do they merge - they just overwrite files.

NEVER run Unity as admin! Unity warns you about this, for good reason. Even a simple scripting mistake could delete system or personal files with no warning, no going back. Plus the usual, you know, hackers love it. Scammers, Phishers, Bitcoin Miners, etc etc

If it’s “just” the OS account then okay, as long as UAC is not force disabled.

What do you need this for? I have a hunch that it’s either an inadequate solution, or wholly superfluous because Unity provides better tools for the job (ie AssetDatabase if this is for editor).

At runtime, there shouldn’t be any need to “watch” the user’s file system. You can rely on things like the app losing focus, while the user copies some music to his music folder for instance, and then tabs back into the game.

1 Like

I’m using this for a Twitch Stream feature which sends the username of the person who sent the command to a txt file which would then be read into a string while also activating a prize wheel that spins sending that data to another txt file to then be read by streamerbot.

so the connection flowchart would look like: twitchAPI → streamerbot → .txt1 → unity → .txt2 → streamerbot → twitchAPI

I’m aware that websockets would be a better solution, it’s on my list of things to learn, but I’d just like to get something working quickly so that I can get both endpoints operating. then I’ll at least know that while implementing websockets everything else is up and running as need be.

TLDR this is all local, yes I need to know if the file was modified, it’s not an adequate solution, it’s an incredibly jank one but it will do XD

  1. Unity is not stored on a cloud drive, it’s local
  2. I am the admin of my system is all that I mean
  3. the user is me, I don’t care if I need to watch my file system lol it’s what I’m doing for this silly little project to entertain maybe 15 people.

I would like to know how I can fix this I have been on the forum trying to resolve this issue for the whole day. please, anyone lmao

Why would it run in the scripts folder? once compiled there is arguably no scripts folder, mostly the editor runs from assets, but, other things can move it, you can change… ask it where it is, hence my point, it can surprise you

So, most likely its c::\users\yourname\documents\unity\project\assets

but, it might not be, so, check where it is… also note, when its built and run it wont run from there and that scripts folder will be only a distant dream… as your build has no structure like that.

so for the build I would need to make it create a file within its dir, in order to ensure it can call/watch it without this sort of issue?

yes it’s within the assets directory. (checked the path of the script)

to make clear the use case

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class leftWheel : MonoBehaviour
{
    public float genSpeed;
    public float subSpeed;
    public bool isSpinning = false;

    public bool changed;
    public FileSystemWatcher fileWheelInput;

    // Start is called before the first frame update
     void Start()
    {
        FileSystemWatcher fileWheelInput = new FileSystemWatcher(@"C:\Users\user\Documents\Streamerbot\redeem files\used_wheel.txt");


            fileWheelInput.NotifyFilter = NotifyFilters.LastWrite;

            fileWheelInput.Changed += OnChanged;

    }

    // Update is called once per frame
    void Update()
    {
        if (isSpinning == true) 
        {
            transform.Rotate(0,0,genSpeed,Space.World);
            genSpeed -= subSpeed;
        }
        if (genSpeed <= 0)
        {
             genSpeed = 0;
            isSpinning = false;
        }
         if (changed)
        {
            SpinWheel();
            changed = false;
        }
    }

    public void SpinWheel()
    {
        genSpeed = UnityEngine.Random.Range(2.000f, 5.000f);
        subSpeed = UnityEngine.Random.Range(0.003f, 0.009f);
        isSpinning = true;
    }

     public void OnChanged(object sender, FileSystemEventArgs e)
    {
        changed = true;
    }
}

that looks feasible

so if handled via debug with

UnityEngine.Debug.Log("Current directory path: " + Directory.GetCurrentDirectory());

it just spits out the main directory of the project.

which we knew it would do.

I feel trolled, this is the dumbest, most ambiguous exception I’ve ever dealt with