Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple C# application not working correctly, works fine when run from within Visual Studio

I'm new to C# and VS, and I have a bizarre problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ExampleApplication {
    class Program {
        static void Main(string[] args) {
            FileInfo fi = new FileInfo("C:\\TEST.TXT");
            fi.Create();
            Console.WriteLine("File exists: {0}", fi.Exists);
        }
    }
}

This is my program, it creates a file TEST.TXT. When I run the program inside VS 2010 with the Debug -> Start Debugging command (F5), the program works fine and the file is created.

However if I build the solution then run the program from the .exe using cmd prompt:

C:\...\Visual Studio 2010\Projects\ExampleApplication\ExampleApplication\bin\Debug\ExampleApplication.exe

It runs outputting: File exists: true. But the file is not created. Anyone have any ideas?

Thanks

like image 261
Ricky Jones Avatar asked Dec 07 '22 15:12

Ricky Jones


1 Answers

Perhaps file stream eventually was not released by OS so file system does not reflect recent changes? Just try explicitly closing a new file stream after creation:

FileInfo fi = new FileInfo("C:\\TEST.TXT");
using (var stream = fi.Create());

Please let us know whether it works or not since it is a bit crazy idea ;) I believe problem could be even simple

like image 138
sll Avatar answered Jan 05 '23 01:01

sll