A simple C # application does not work correctly, works fine when launched from Visual Studio

I am new to C # and VS, and I have a weird 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 TEST.TXT file. When I run the program inside VS 2010 using the Debug β†’ Start Debugging (F5) command, the program works fine and the file is created.

However, if I create a solution, run the program from .exe using the cmd prompt:

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

It launches the output: File exists: true . But the file was not created. Does anyone have any ideas?

thanks

+4
source share
10 answers

Perhaps the file stream was not finally released by the OS, so the file system does not reflect recent changes? Just try explicitly closing the new file stream after creating:

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

Please let us know if this works or not, as this is a bit crazy idea;) I believe the problem may even be simple

+3
source

I suspect that the user account control redirects the creation of the file to the shadow copy directory; under VS, you work as an administrator, so the creation of the file is not redirected.

Look for the file in the VirtualStore directory. It will be

 C:\Users\YourUserNameHere\AppData\Local\VirtualStore 

The actual folder will look like

 C:\Users\YourUserNameHere\AppData\Local\VirtualStore\C 

To solve this problem, you can change the program so that it writes the file to a place intended for user data; the obvious choice would be the Documents folder. You can also run the application as administrator. There are at least three ways to do this:

  • Right-click the executable file and select Run as Administrator
  • Run the executable file using the shortcut after checking the "Run as administrator" checkbox in the shortcut properties dialog box
  • Use one of the methods above to open an Administrator command line session; then run the application normally by typing its name on the command line.
+3
source

There may be a problem with permissions. If the user account using vS is different from the account that runs the command line, they may have different permissions to create files. Look in your Windows Event Viewer to see if the error is listed there. Then use try-catch, as Mark suggested.

+2
source

Just a hint. When you work with files, always do this in a try catch. You never know what might happen, as in your case.

Are you running cmd as an administrator? You may not have permission to create the file.

Also add a catch attempt and return an error in the console. This will help you sort out your problem if working with an administrator does not work.

  try { your code.. } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } 
+1
source

I don’t know the reason for this, but why don’t you try this:

 if(!File.Exists("C:\\TEST.TXT")) { File.Create("C:\\TEST.TXT"); Console.WriteLine("File exists"); } 
+1
source

You need to call fi.Refresh (); so that FileSystemInfo re-examines the underlying object, otherwise it will always return false.

+1
source

Try closing the file as follows:

 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(); fi.Close(); Console.WriteLine("File exists: {0}", fi.Exists); } } } 
+1
source

You are doing it wrong. After that you should delete the file. To create an empty file in .NET, I suggest you do it like this:

 using (File.Create("C:\\TEST.TXT")); 

or like this:

 File.Create("C:\\TEST.TXT").Dispose(); 

Alternatively, you can simply change your fi.Create() to:

 fi.Create().Dispose(); 

It is likely that Visual Studio Debugger automatically clears your clutter for you by correctly deleting all objects when you stop debugging.

+1
source
 use the following code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ExampleApplication { class Program { static void Main(string[] args) { if(!File.Exists("C:\\TEST.TXT")) { GC.Collect(); GC.WaitForPendingFinalizers(); fi.Create(); fi.Close(); Console.WriteLine("File exists: {0}", fi.Exists); } } } } 
+1
source

This is a simple solution,

I ran the code and received a UnauthorizedAccessException, which is a security exception, you do not have the correct security to save the file in the root folder (C: \)

if you change it to FileInfo ("TEXT.TXT"); instead of C: \ TEXT.TXT it will work

Btw, you cannot use the using statement because FileInfo does not implement the IDisposable interface.

+1
source

Source: https://habr.com/ru/post/1400899/


All Articles