Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have one batch exe along with files.i want to copy files to located path from form1 button click.after compile when i click button its working but if i take form1 application to desktop then i tried click button but files are not copy to path.

below is my tried code.

pls help

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            //File path where you need to copy
            string configFilePath = @"C:\Windows\Files\";
            try
            {
                if (!Directory.Exists(configFilePath))
                {
                    
                    Directory.CreateDirectory(configFilePath);
                }
                //File path where you are stored in the solution itself
                string SourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Files\");
                string[] sourceFiles = Directory.GetFiles(SourceFilePath);
                foreach (var file in sourceFiles)
                {
                    File.Copy(file, configFilePath + Path.GetFileName(file), true);
                }
            }
            catch (Exception)
            {
            }
Posted
Updated 27-Aug-19 21:29pm

We can't help you with this: we don't have access to the stings, or to the file system to which they relate.
Assuming the desktop doesn't have a debugger installed (if it does, just use it, it'll be much, much faster) add logging to you code above to record the various strings, and the path the code takes through the code. You can then review that after a failure to find out what the code expected to be where, and check that against the file system.

But ... don't swallow exceptions: the most likely thing to be happening here is that something in there is throwing an exception (quite likely CreateDirectory) and you are discarding that information in the catch block. try ... catch isn't a "miracle cure" for problems, it's there to help you handle them and fix the actual problem. Swallowing the exception instead of reporting or logging it just means you have no idea why a problem happened, and often even if it happened until it's too late.
 
Share this answer
 
The AppDomain.CurrentDomain.BaseDirectory changes if you move the executable. There are great chances that the desktop does not contain any "Files" directory.

You can craft the SourceFilePath variable so that it no longer depends on the location of the executable (i.e., provide a static full path to this variable).

Or you can create a shortcut on the desktop (instead of copying the executable).

Second way may be shorter/simpler, if you do not plan to use the executable on another computer.

But if you do plan to use the application on another computer, you will definitely have to rethink the way your are building your SourceFilePath.

Hope this helps.
 
Share this answer
 
Comments
Member 10570811 28-Aug-19 3:35am    
yes,i need to do plan to use this application on another computer.in that basis i tried,but i stuck here
phil.o 28-Aug-19 3:42am    
So provide a setting in your application where you will be able to customize the source files' path per user/computer.
Here you will find a guide on this functionality which is rather quick to implement once you have understood how to use it.
Using Application Settings and User Settings
I promise you, it's worth the look.

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