octopusgrabbus
octopusgrabbus

Reputation: 10695

Access To The Path Is Denied From F#

I have read this post. I am trying to move a file from one place to another on a Windows server. The user running the F# program has full privileges to execute the .Net File.Move statement and I tested this move manually using the same user logged into the server using File Explorer.

I am trying to figure out what I am doing wrong.

Here is relevant code, and copyOnly is false.

let rc =
    try
       if copyOnly then
          File.Copy((currentPath + fnam), (destPath + new_file_dest)) |> ignore
          logH.WriteLine(generateTimeStamp () + ": " + currentPath + fnam + " copied to " + (new_file_dest + fnam))
       else
          File.Move((currentPath + fnam), (destPath + new_file_dest)) |> ignore
          logH.WriteLine(generateTimeStamp () + ": " + currentPath + fnam + " moved to " + destPath + new_file_dest)
                true 
    with ex -> 
       logH.WriteLine(generateTimeStamp () + ": " + "An error occrred trying to move or copy " + currentPath + fnam + " to " + (destPath + new_file_dest))
       printfn "%A" ex
       false
rc

currentPath = \\\\Munis2\\musys_read\\musys\\import_prod\\
fnam = import_arlckbox.007
destPath = \\\\Munis2\\Munis_IC_DataExchange\\archive\\
new_file_dest = import_arlckbox_6132018_15645.007

I am stumped as to why this is a problem. Looking at the other article (post), I did make sure both users I am testing with have full privileges in all the directories, and I am moving one file to another file name in another directory. That was one of the resolutions in the referenced article.

I have a second question related to the error. If I was trying to debug this problem using URL path syntax, as indicated above, which user is trying to perform this task on the server?

Is the user the same user as on the workstation debugging in Visual Studio, or is the request to move or copy handled by a default internet user?

I am asking this, because after fixing the corrupt path problem, I was still getting the access to the path is denied error debugging using URLs, but not when the application was running directly on the server.

Upvotes: 1

Views: 352

Answers (2)

octopusgrabbus
octopusgrabbus

Reputation: 10695

First, there was a path problem. The partial path (without the file name) was being copied into the full path with the file name. Once the application's Access database (source of the paths used by the program) was set back to using standard Windows drive paths -- G:\dir1\dir2\file1.007 and the invalid path was fixed, the program went back to normal.

It would still be nice if File.Move (or Copy) would provide a more robust error.

Upvotes: 1

karthik Mushyam
karthik Mushyam

Reputation: 23

I don't know F# but in Windows app or Windows Server you need to use Application ExecutablePath and Remove bin\Debug as shown

 string startupPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath) + "\\Templates");

 startupPath = startupPath.Replace("bin\\Debug\\", string.Empty);

 var newfileName2 = Guid.NewGuid() + ".xlsx";

 string destinationPath = Path.Combine(startupPath, newfileName2);

 File.Copy(sourcefile, destinationPath, true);

This is C# code. Posted this code to get some idea for your Question

Upvotes: 1

Related Questions