Finding a directory or file without name? - c#

We have a some files or directories with their absolute paths.
These files or directories will be renamed in a process. We can't get the new names from the process at all, but we have the root directory full name(full absolute path)
Now, I wanna find that items again
Is there a unique key or something for directories or files to find them without the exact name?

No, there is nothing directly available on regular NTFS from C# code to do so.
You can
compute some sort of hashes of files yourself + size check to find them again after rename (if they simply renamed)
use events from FileSystemWatcher to track file movements
add alternative streams to files if they will not be stripped by "the process" to use as custom markers.

Related

Tracking the location of a file in C# windows?

I need to track the location of some files that i know the initial full paths. Situtation as follows :
I have a file in path C:\Temp\first.txt
in some time this first.txt file changes location to for example C:\Temptwo folder
i need to learn this second location automatically when the location change happens is there way to do it in C# thanks.
It depends on where the files are moving from and to. If you are moving from C:\\MyProgram\TempA\Temp.txt to C:\\MyProgram\TempB\Temp.txt, then you can just use the directory tools. Directory.GetFiles will scan a directory for you and return an array of strings containing the names of those files. If you know the name of the file you are looking for, or you are using a unique file extension, then you can search through the files you find until you identify the file you are looking for. You can also call Directory.GetDirectories recursively to look through sub-directories if you have a lot of folders this file could be hiding in.
While this might technically work on the C drive, I would suggest carefully considering your use case before trying to re-invent Windows Search.

File path for c# when moved to a memory stick

sorry first time user and currently learning c# at uni, i'm working on an assignment and i'm trying to get the file path to work on the memory stick as that's what i need to hand it in on, thanks many regards
I'm not completely sure what your question is by I think what you're talking about is absolute vs relative paths. If you use an absolute path like "C:\users\yourname\blalba\project\stuff", then it's obviously only going to only work on your computer. However, you mostly all of the time want to use relative paths. Relative paths have the root directory of the build output files for your project; where your .exe file is built for your project. This is usually in "projectdir\bin\debug" or "projectdir\bin\release". So if you put for example a file called 'test.txt' in that directory, you can simply put the relative path "test.txt" instead of "C:\users\yourname\blalba\project\bin\debug\test.txt". If you were to put 'test.txt' in the project directory, you can use the relative path "....\test.txt". "..\" means navigating one step back.
The path to the directory containing the files you are looking for is "F:\Mod005244, 1715840". The "1715840 JH" is just the name of the drive. You access different drives via the drive letter. In this case, the drive letter "F" was assigned to your flash drive.
I would suggest making the file path configurable or request input from the user of the program as the drive letter of a flash drive will not always be "F". There are even classes (such as FileBrowserDialog) that will open a graphical file browser dialog and prompt the user to navigate and select a file.

File read/write paradigms on Unity- which to use?

I want to load some JSON files that are stored in my assets.
I don't know where I'm supposed to store them in the assets. Should I store it in the "Resources" folder, in the "StreamingAssets" folder or somewhere else ?
I don't know what path I'm supposed to use. I heard you need to write : path = "jar:file://" + Application.dataPath + "!/assets/"; for the StreamingAssets but there is also Application.streamingAssetsPath. I also heard you always need to use Application.persistantDataPath. So which path should I use ?
I don't know what I can use in to access the files in C#. I've heard you can use Directory.Exists() and File.Exists() but if you use the StreamingAssets you must use WWW. Is that right?
You can get a basic idea in this answer.
In addition, I would suggest placing anything you want to package with your game in your Resources folder, which you can access by using Resources.Load(). You can iterate through these directories however you want- ex: Resources.Load("folder/file");.
If you want to write files, they have to go in Application.persistentDataPath as that's the only write-able directory (that's specified by Unity), i.e. for run-time files only. But once a file is stored there, it is persistent between sessions.
You can, of course, specify absolute paths and try to load assets from there, but that's more risk than necessary (paths differ on different devices).
You could ignore Application.dataPath altogether, until you find a specific need for it (ex: to access files that are not in persistentDataPath, but not in resources either).
I hope that helps!

FileSystemWatcher Filter - Detect Zipped Files?

I'm using a FileSystemWatcher to detect that a text file is created in directory A and subsequently created in directory B.
The issue I'm having is, the process which moves the file from directory A to directory B also zips the file up, changing the filename from say "999_XXX_001.txt" to "999_XXX_001.txt.zip"
Three problems with this;
1) I can no longer open and read the file to analyse the contents
2) The filename has changed
3) The FileSystemWatcher appears to only support a single extension
Solution
Using two watchers, one for ".zip" and one for ".txt", I'm removing the .zip and comparing filenames because moved files no longer exist to be compared byte-for-byte.. I guess the real question here was how can I use the watcher to detect ".txt.zip" as an extension!
Why? You would have to wait until the process has finished its zipping magic and afterwards you can open the zip file with your framework of choice
Why is it a problem itself that the filename has changed?
No, the file watcher will detect any change of all files within the given directory
But maybe it is better to describe what you actually try to achieve here. There is probably a better solution to what you actually need.

Watching a directory using FileSystemWatcher

Hello I'm watching a directory using FileSystemWatcher.
When a file is created into that directory - my watcher grabs and transfers it to the network drive.
My problem is that when a Microsoft-office file is opened, a temporary file is created in the watched directory. I can't find a way to ignore these files and also could not find when I should move these file to the network drive.
Temporary files have file attribute that cite it.
You can check for this attribute if the FileInfo.Attributes
check this FileAttributes
If I'm not mistaking, those temporary files are hidden files. If that doesn't work, you might consider allowing files with special names (those temporary files start with a tilde ~) to be ignored by your program. Using Regular Expressions you could set more "ignore masks" like that.

Categories