0

I am somewhat new to C# but not new to coding itself. I am currently trying to write a console app to run some processes, gather information from those processes, and then write the values to a .txt file.

I have everything running smoothly in Visual Studio 2017 RC but when I publish and run the program everything runs except the part where the values/data is published to a document on my desktop. Can someone point me in the right direction or inform me why it is working in Visual but not in the program?

public static bool WriteData(string DataNeeded)
    {
        string Root = @"\text.txt";
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Root;

        if (!File.Exists(path))
        {

            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Header of TXT file");
                sw.WriteLine("Document created: " + DateTime.Now);
            }
        }

        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine("Data being written to text file: " + DataNeeded+ " " + DateTime.Now);
        }

        return true;
    }
Inneshfree
  • 67
  • 1
  • 9
  • Does the document not get created at all? What user is the program running as? – D Stanley Jan 20 '17 at 21:37
  • Does it throw any errors or if program crashes? The best bet would be it has something to do with the access, does it creates the text.txt on desktop? – Ali Baig Jan 20 '17 at 21:38
  • *"I publish and run"* -- By chance are you running as a scheduled task? You'll have to set it up with a little bit more permissions I believe. – TyCobb Jan 20 '17 at 21:39
  • @D Stanley The program does not crash or throw any errors, and in visual studio the text file is created but not when the program is published as a package. @ALI Baig The program would be run as whatever user was running it. – Inneshfree Jan 20 '17 at 21:41
  • @TyCobb currently I am not using task scheduler to run this. In the future maybe but right now this is a double click to run process. – Inneshfree Jan 20 '17 at 21:42
  • Is it anything to do with Desktop VS DesktopDirectory? http://stackoverflow.com/questions/5612571/whats-the-difference-between-specialfolder-desktop-and-specialfolder-desktopdir – Christopher Thomas Jan 20 '17 at 21:44
  • @Inneshfree try to create a file in some other directory or give the current user permissions to create the file. – Ali Baig Jan 20 '17 at 21:44
  • @Christopher Thomas - That fixed the issue! I was using the logical desktop space rather than the file system location. IE: Desktop instead of DesktopDirectory. – Inneshfree Jan 20 '17 at 21:50
  • Cool, glad it helped. – Christopher Thomas Jan 20 '17 at 21:54

1 Answers1

3

I cant believe the solution was that simple. I was using a logical desktop space reference rather than using the file system location

string Root = @"\text.txt";
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + Root;

The above SpecialFolder.Desktop should have been:

string Root = @"\text.txt";
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + Root;
Inneshfree
  • 67
  • 1
  • 9
  • 1
    As a general rule of thumb, use [Path.Combine()](https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx) when building paths as in `string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Root);` This will prevent double backslashes, or possibly no separator ending up in your final path. – Idle_Mind Jan 20 '17 at 22:01