Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have utility to rename a file in a specified directory using a certain condition. Running the code using a console application works well and the file is renamed appropriatelty. However when i attempt the same in a web application the file is not getting renamed. I am using VS2017 Development Server for the web application debug.

What am i missing?

Using the console application code as below the file successfully gets renamed :

Rename method:

C#
public static string AddSuffix(string filename, string suffix)
       {
           string fDir = Path.GetDirectoryName(filename);
           string fName = Path.GetFileNameWithoutExtension(filename);
           string fExt = Path.GetExtension(filename);
           string renamedFilePath= Path.Combine(fDir, String.Concat(fName, suffix, fExt));
           return renamedFilePath;
       }


Usage in working main program:

C#
<pre>static void Main(string[] args)
        {
		 string batchperiod = "_70_";
            string realPath = @"C:\Users\myuser\source\repos\Solution\Project\BatchIn";
            IEnumerable<string> fileList = Directory.EnumerateFiles(realPath);

            var CurrentBatchName = (from file in fileList
                                    let fileName = Path.GetFileName(file)
                                    where fileName.Contains(batchperiod)
                                    select fileName).FirstOrDefault();

            string absolutePath = (@"C:\Users\myuser\source\repos\Solution\Project\BatchIn\" + CurrentBatchName);
            string newPath = Helpers.AddSuffix(absolutePath, String.Format("({0})", Helpers.parameters.IsProcessed));
 
            System.IO.FileInfo fi = new System.IO.FileInfo(absolutePath);
            if (fi.Exists)
            {
                fi.MoveTo(newPath);

            }
}


With this code the file is successfully renamed from
GL_Export_70_201907081058.xml
to
GL_Export_70_201907081058(P).xml


The only difference using web application is that the absolutePath is stored in a Session variable having been derived from a preceding operation/ActionResult :

C#
var absolutePath = (@"C:\Users\myuser\source\repos\Solution\Project\BatchIn\" + CurrentBatchName);
                   files.FileName = CurrentBatchName;
                   Session["AbsoluteBatchPath"] = absolutePath;


and later invoked in another ActionResult as :
C#
var sourceFile = Convert.ToString(Session["AbsoluteBatchPath"]);
                
                string newPath = AddSuffix(sourceFile, String.Format("({0})", parameters.IsProcessed));
              
                System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
                 
                if (fi.Exists)
                {
                    // Move file with a new name. Hence renamed. 
                    fi.MoveTo(newPath);
                }


What I have tried:

I have tried using File Move variant but it cannot work with FileInfo class. Anyone to help on why the function is not working in the web application. I suspect it might have something to do with IISExpess that runs with Visual Studio.

Are there any permissions i need to alter at IISExpress level.
Posted
Updated 28-Jan-20 23:47pm
Comments
Richard MacCutchan 22-Jan-20 4:04am    
You are not allowed to change anything in the client from a browser. Would you like to go to a website that can rename files at will on your system?
Tshumore 22-Jan-20 4:49am    
Ok i get it. So what permissions do i need to change, in which folder, to enable this
Richard MacCutchan 23-Jan-20 4:24am    
Read my comment again: browsers are not allowed to change anything on the user's system.
_Asif_ 22-Jan-20 6:37am    
You probably need to give full access rights to IIS_IUSRS User
[no name] 22-Jan-20 13:15pm    
Why are you accessing (windows) "user" files on the server? Web applications use "application data" (folders) and / or data bases.

1 solution

I'd strongly recommend to read this: Development-time IIS support in Visual Studio for ASP.NET Core | Microsoft Docs[^] and this: Publish to IIS by importing publish settings - Visual Studio | Microsoft Docs[^]

You need to understand (what has been already stated in the comments) that accessig files in design mode and "production" mode is totally different.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900