0

I did find a few similar questions, but they weren't able to point me in the right direction... This may be something entirely stupid, but if anyone could tell me why I can't get a string populated I'd appreciate it. Here's my method that's failing:

private static string passwordTrace { get; set; }

// ... lots of other code

private static void RefreshPassword()
    {
        try
        {
            string filePath = "\\\\[server ip]\\share\\folder\\file.abcd";
            string npDecrypted;
            DateTime lastRefreshDate = Properties.Settings.Default.lastRefresh;
            if (DateTime.Now >= lastRefreshDate.AddDays(30))
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    string npEncrypted = sr.ReadLine();
                    if (npEncrypted.Length != 24)
                    {
                        string fr = File.ReadAllText(filePath);
                        npEncrypted = fr.Substring(0, 24);
                    }

                    npDecrypted = Decryptor(npEncrypted);
                    passwordTrace = npDecrypted; // added for debugging only! remove when done.

                    secureString npSecure = new SecureString();
                    foreach (char c in npDecrypted)
                    {
                        npSecure.AppendChar(c)
                    }
                    Properties.Settings.Default.adminpw = npSecure;
                    Properties.Settings.Default.Save();
                }
            }
        }
        catch (FileNotFoundException fnfe)
        {
            // code for handling this type of exception
        }
        catch (NullReferenceException nre)
        {
            // code for handling this type of exception
        }
        catch (Exception e)
        {
            // code to catch ANY other type of exception
        }
    }

Now, there are no errors or warnings when the VS debugger compiles everything, and it works correctly when debugging. But, if I copy the compiled exe (from C:\project\bin\Debug directory) and run it the issue arises.

The point that says passwordTrace = ... is called at another point by a message box. This works correctly when running via debugger and there aren't any exceptions thrown anywhere (I do have try/catches all over the place), but for whatever reason the PasswordTrace and the Properties.Settings.Default.adminpw don't seem to be holding their value throughout the applications execution.

Also, the file that is being read is an encrypted text file which will always have only 1 line of characters and that line is always 24 characters long. An example would be:

09sdjf09ausd08uf9!%38==

As a final statement, I also copied the app.exe.config and app.pdb to the server directory where I copied the compiled .exe file to see if that had anything to do with it and it didn't fix anything. I also tried running the .exe directly from the Debug directory (the same file that I'm copying elsewhere) and it works correctly. As soon as I move it off of the Local Disk it doesn't work.

So, my suspicions are that it has something to do with the environments working directory, or something to do with how the app is executing. I read something somewhere that noted the default users is not set, but I think that was specifically regarding ASP.NET. If that was the case and the user double-clicking on the .exe didn't have a proper network authentication then what would I do? And if it has something to do with the working directory, how can I circumvent this?

I'm gonna keep fiddling and if I figure it out I'll update this, but I'm so lost at the moment! lol! And for the last time - everything it/was working correctly until copying it to the server location.

Thanks in advance! :)

leppie
  • 115,091
  • 17
  • 196
  • 297
kyle_engineer
  • 280
  • 1
  • 11
  • It returns `null` when it reaches EOF. So you file is likely empty (I think) – leppie Jun 15 '16 at 19:01
  • add a messagebox that will output the configuration setting(s) you expect (`lastRefresh`). sure that none of the exception `catch` blocks are mute (asking since they're empty in the snippet)? Try hardcoding the configuration property values for a test... if it works, then the configuration file might have issues. – Cee McSharpface Jun 15 '16 at 19:06
  • .NET historically enforced all sorts of security measures [when running from a network drive](http://stackoverflow.com/a/148900/21475) (i.e. you couldn't do anything). Are you sure this isn't the problem here? – Cameron Jun 15 '16 at 19:11
  • [true](https://blog.coolmuse.com/2012/03/15/net-security-exceptions/). CAS was simplified greatly starting from .NET 4.0, but: there should be a `System.Security.SecurityException` then, never a silent failure. – Cee McSharpface Jun 15 '16 at 19:20
  • @Cameron that's kinda one of the things I was looking at, but as dlatikay said, that *should* throw an exception... I'm thinking the starting dir is affecting it traversing the folder tree properly... But since the path is a complete path I don't see that happening either... – kyle_engineer Jun 15 '16 at 19:28
  • You should add some logging in your code. Look at [this](https://www.youtube.com/watch?v=2lAdQ_QwNww) hands-on, for example. – Jeroen Heier Jun 15 '16 at 20:34
  • @JeroenHeier I have added more, but there's still not any kind of error or event being generated... – kyle_engineer Jun 15 '16 at 21:07
  • Ok, so upon closer inspection, it looks like the Properties.Settings.Default values are hanging around forever and even when I'm deleting the .exe file on the server and replacing it with a new one, the old data is persisting... wtf!? But this seems to be the problem. – kyle_engineer Jun 15 '16 at 21:09
  • You didn't actually explain where you are seeing the problem or what the problem is. Your title says `StreamReader` and `File.ReadAllText` are returning null or empty, but the body of your text says "PasswordTrace doesn't seem to be holding its value" and "I can't get a string populated". Have you actually seen `StreamReader` return `null`? Or do you just get a MessageBox that's empty, therefore "PasswordTrace must be empty, therefore my readers aren't working correctly and must be returning null or empty"? Note that your example is 23 characters long, not 24; `Substring(0, 24);` will throw. – Quantic Jun 15 '16 at 22:17
  • @Quantic since it was only happening after it was compiled, I couldn't trace where what was going wrong... :/ but, I was able to trace down several problems... By basically rewriting everything! LOL! I'll update it tomorrow when I'm back at my computer. – kyle_engineer Jun 16 '16 at 01:44
  • you should not be silently ignoring exceptions – pm100 Nov 26 '22 at 23:03

0 Answers0