System.IO.Directory.GetDirectories not working after publish - c#

I have a C# application that gets a list of directories inside a folder. This is done using the call
String[] projects = System.IO.Directory.GetDirectories("path/to/folder", "*", System.IO.SearchOption.TopDirectoryOnly);
This works fine on my machine, but after publishing (resulting in a setup.exe, as well as programName.application + Application Files) I tried running the program on a new machine and it threw an unhandled exception error.
The error was in regards to being unable to connect to a database, but the interesting part is that it was complaining about path not being valid, listing a path that only exists on my machine.
Does System.IO.Directory.GetDirectories not get reinitialized when running on another machine?

I guess the problem is with path/to/folder, as that path might not exist in new machine. Don't hard-code the path. Instead read it from config file (app.config using ConfigurationManager).

Related

UnauthorizedAccessException in UWP Directory.CreateDirectory

This is a Unity project building to Hololens 2. I'm trying to create a new folder within the Application.PersistentDataPath but it's failing due to UnauthorizedAccessException. Strange thing is it's been working and only recently with seemingly unrelated changes it stopped.
Here's the function that is failing.
static DirectoryInfo EnsureDirectory(string subFolder)
{
Debug.Log($"starting EnsureDirectory() for {subFolder}.");
string directoryPath = Path.Combine(Application.persistentDataPath, subFolder.ValidatePath());
Debug.Log($"About to create directory {directoryPath}");
var dir = Directory.CreateDirectory(directoryPath);
Debug.Log($"Successfully created directory {dir.FullName}");
return dir;
}
At Directory.CreateDirectory I get the error as follows:
Hololens screenshot of debug log
EXCEPTION: UnauthorizedAccessException: Access to the path "C:\" is denied.
This error only happens when deployed to the device. In the Unity editor it works perfectly. I also don't know why it would say "C:" when that's not the path I'm trying to use.
Any help would be greatly appreciate.
Thank you #aybe for the answer that did the job.
This is probably because it checks for every path segment, starting
with the first one which is root and obviously forbidden. Try new
DirectoryInfo(Application.persistentDataPath).CreateSubdirectory(...);
instead to see if the error vanishes

Unable to access network path using WMI managementClass.InvokeMethod

I have been trying to launch text pad installation from network path to a remote machine using WMI managementclass.InvokeMethod.
However several attempts have failed with
error code 1602
So I created a batch file containing script to launch installation executable from network path in a remote machine, and tried to invoke it with managementclass.InvokeMethod.
However, even this didn't work.
I tried the same by changing the executable from network path to remote machine C:\path.
And it worked!!!
But I want to launch the installation executable from a network path.
I would be grateful if somebody would help me.

PrivateFontCollection.AddFontFile() throws generic GDI+ error exception

Adding a private Font works fine on my local dev machine with Windows 10 Pro. However on Windows Server 2012 R2 the very same code results in the following error:
System.Runtime.InteropServices.ExternalException: A generic error
occurred in GDI+. at
System.Drawing.Text.PrivateFontCollection.AddFontFile(String filename)
var privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile("Roboto-Regular.ttf");
When I delete the file it results in a NotFoundException, so the file path has to be right. I ensured that every user has every possible permission on the file. (I found out that if I deny every permission on my local win 10 machine the same "generic error" occurs - so it might be a permission problem)
Has anybody a solution for this problem?
Could it be possible that the service user on the productive machine lacks some other rights that are necessary for "installing" private fonts? Maybe the font gets installed to a specific directory which is not allowed - but I could not find out where this would be.
Please help me with this very self explaining "generic error".
It seems to have been a problem with not using an absolute path. Didn't think of it at first, because deleting the file resulted in an explicit FileNotFound.
Using an absolute path did the trick.

Access to the path 'autorun.inf' is denied when File.Copy is executed

I get the following exception when I try to copy a file in my Windows Application...
Access to the path 'autorun.inf' is denied.
The file I am trying to copy is a shortcut file. However when I move the EXACT line of code in a standalone application, it executes flawlessly. Same user credentials. There is no difference on how I launch either application.
This is the line of code...
File.Copy(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "JEE Startup.lnk"), #"C:\Documents and Settings\All Users\Start Menu\Programs\Startup\JEE Startup.lnk", true);
I tried deleting the bin folder and rebuilding. No luck.
I have no idea what is wrong.
The application is being executed on an XP machine.

WmAutoUpdate - anyone used it? Won't roll back

I've built a Compact Framework application and I'm using WmAutoUpdate to deploy new versions to the mobile devices (http://www.sebastianvogelsang.com/2009/09/23/wmautoupdate-a-net-compact-framework-auto-update-library/). Has anyone used this? It's cool but I've got a problem.
If I cause the application to crash half-way through updating it is supposed to recover by copying the backup version back into the main directory. This doesn't work because the exe file is "locked" by the operating system because it is currently in use. I can verify this is the case because I can't delete it using Windows Explorer either. The error details are:
System.IO.IOException was unhandled
Message="IOException"
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at WmAutoUpdate.Updater.assertPreviousUpdate()
at WmAutoUpdate.Updater..ctor(String url)
The error occurs on this line in Updater.assertPreviousUpdate():
File.Move(f, appPath + "\\" + getFilenameFromPath(f));
The code manages to update the application exe file when it's allowed to run normally (I'm not sure how). The problem is that it doesn't work when rolling back.
Cheer
Mark
I've used WmAutoUpdate and I've found the same problem. The issue is that you can move the files of the actual running process, but you cannot overwrite them. If you check the update part, WmAutoUpdate moves the running application to a backup directory and then it writes the update version to the original directory. I have fixed the rollback part this way:
if (Directory.Exists(backupDir))
{
string tmpDir = Path.Combine(Path.GetTempPath(),Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
Directory.Move(appPath, tmpDir);
Directory.Move(backupDir, appPath);
}
First we move the running application files to a random directory in Temp. Then we copy the backup folder to the application original directory. Of course, this will generate a .TMP file in the Temp directory of your device, and a folder with the actual running process. You will have to delete this temporary folder once in a while in production code.

Categories

Resources