0

I have simple code for creating .sql file in some directory.

string path = @"C:\Examples\my projects\Database\1.0.0_FirstScript.sql";
File.Create(path);

I have checked my folder Database and every user has full control for that directory. But when I try to create file on this location with some dummy extension like .lkp it works, but when I try to create with .sql it doesn't work.

And in Command Line I get this error:

Unhandled exception: System.UnauthorizedAccessException: Access to the path 
'C:\Examples\my projects\Database\1.0.0_FirstScript.sql' is denied.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
   at System.IO.StreamWriter.ValidateArgsAndOpenPath(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.File.WriteAllText(String path, String contents)
   at imt.CLI.Services.MigrationService.AddMigration(ApplicationSettings applicationSettings, AddMigrationDTO addMigrationDTO) in C:\Project\C#\MigrationService.cs:line 30
   at test.InvokeAsync(InvocationContext context) in C:\Project\C#\AddMigrationCommand.cs:line 39
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass17_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass12_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass19_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__18_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__5_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass8_0.<<UseExceptionHandler>b__0>d.MoveNext()

I am running this program as CLI using System.CommandLine, and I tried to open CMD as Admiistrator and witout Administrator but there is no diference, just for some reason it want allow me to create .sql file.

I have tried on different location on disk, but behavior is the same on every location.

Does someone now can I check to see what is real reason for getting this error, because this is just UnAuthorized Exception for that throwing can be many reasons?

savke
  • 268
  • 1
  • 3
  • 11
  • I have to ask, does the parent structure `C:\Examples\my projects\Database` already exist? – Compo Nov 06 '22 at 15:30
  • 1
    Seems like incorrect usage. The following may be helpful: [File.Create](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.create?view=net-6.0#system-io-file-create(system-string)), [Fiile.WriteAllText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-6.0), and [File.WriteAllLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealllines?view=net-6.0) – Tu deschizi eu inchid Nov 06 '22 at 15:32
  • @Compo yes, it exists. I created folders, also added all permisions to all users I have on that computer. – savke Nov 06 '22 at 15:34
  • Does adding a file with this name manually work? – Klaus Gütter Nov 06 '22 at 15:34
  • 1
    BTW: `File.Create` opens the file and you seem to not close it again. Better: `File.WriteAllText(path, "")` – Klaus Gütter Nov 06 '22 at 15:36
  • @KlausGütter when I add manually it works, but when I trying through my app it doesn't, I tried with File.WriteAllText, with File.Create and also tried with adding .Close() for closing stream but it doesn't work. I also tried creating it eith using FileInfo class but I get the same exception when I try to save file with .sql extension but other dummy extensio works on the same location. – savke Nov 06 '22 at 15:39
  • @user9938 I haven't tried File.WriteAllLines I will try with this, if you mean as incorrect using because I didn't close stream in this example, I have tried that but also doesn't work. – savke Nov 06 '22 at 15:41
  • 1
    Any super-motivated antivirus software at work? – Klaus Gütter Nov 06 '22 at 15:41
  • @KlausGütter there is Kaspersky but I don't know does he have some settings about this? – savke Nov 06 '22 at 15:44
  • I don't know Ksapersky, but usually you can exclude directories from virus scanning. – Klaus Gütter Nov 06 '22 at 15:46
  • @KlausGütter problem is that I have Kaspersky license managed by organisation, and don't have permission for excluding files or folders. Could anything other be that could gave this behaviour? Is there some way to include in C# to check if that user which is going to create file has permission or something other? – savke Nov 06 '22 at 16:03

0 Answers0