Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Any best approach to change my shitty coding ?

Posted on 2012-03-10
5
Medium Priority
?
237 Views
Last Modified: 2012-03-17
Hi All,

I was using Make directory to create a folders and subfolder in my ftp (using filezilla) works fine,but when i try to do in my test server (IIS FTP) doesn't work ,throws 550,file not found or no access.so just a quick way to change the code to create subdirctory in my ftp server works fine but i know its a kinda shitty way to do like that.

Some one please advice the second thought to approach.

 
   string path = "ftp://1.1.1.1/media/times/" + Name + "/test/fileName";
            string[] pathsplit = path.ToString().Split('/');
            string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3];
            string SecondPath = Firstpath + "/" + pathsplit[4];
            string ThirdPath = SecondPath + "/" + pathsplit[5];
            int count = pathsplit.Count();


            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential("sh", "se");
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    // Okay.  
                    upload();
                }
            }
            catch (WebException ex)
            {
                try
                {
                   //create the first directory if its not there
                   //If already there call the catch
                    FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(Firstpath));
                    createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
                    createdir.Credentials = new NetworkCredential("sh", "se");
                    createdir.UsePassive = true;
                    createdir.UseBinary = true;
                    createdir.KeepAlive = false;
                    FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
                    Stream ftpStream1 = response1.GetResponseStream();
                    ftpStream1.Close();
                    response1.Close();
                }
                catch (Exception e)
                {
                    try
                    {
                       //create the second directory if its not there
                       //If already there call the catch
                        FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(SecondPath));
                        createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
                        createdir.Credentials = new NetworkCredential("sh", "se");
                        createdir.UsePassive = true;
                        createdir.UseBinary = true;
                        createdir.KeepAlive = false;
                        FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
                        Stream ftpStream1 = response1.GetResponseStream();
                        ftpStream1.Close();
                        response1.Close();
                    }
                    catch (Exception el)
                    {
                        try
                        {
   							//create the third directory if its not there
                   //If already there call the catch
                            FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ThirdPath));
                            createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
                            createdir.Credentials = new NetworkCredential("sh", "se");
                            createdir.UsePassive = true;
                            createdir.UseBinary = true;
                            createdir.KeepAlive = false;
                            FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
                            Stream ftpStream1 = response1.GetResponseStream();
                            ftpStream1.Close();
                            response1.Close();
                        }
                        catch
                        { }
                    
                    }
                }

                if (ex.Response != null)
                {
                    FtpWebResponse response = (FtpWebResponse)ex.Response;
                    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                    {
                        // Directory not found.  
                    }
                }
            } 

Open in new window

0
Comment
Question by:Sha1395
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 37705757
It looks more like you may have permissions issues in your server.
Please follow the comments on the link about how to use audit and process monitor
http://forums.iis.net/t/1149116.aspx
0
 

Author Comment

by:Sha1395
ID: 37708391
Hi srinipro,

i don't have issue to upload,i have an issue to create sub dir.
0
 

Author Comment

by:Sha1395
ID: 37708392
@mas_oz2003,

Thank you so much for your comment,am still working with network people based on your suggestion to inherit the permission to all the subfolders.

Thanks Again.
0
 

Author Comment

by:Sha1395
ID: 37708432
Changed my code little bit,this looks some how better than previous,still looking for an expert comment to do this more efficient.

		var dir = new ConsoleApplication5.Program();
            string path = "ftp://1.1.1.1/testsvr01/times/" + "testfile" + "/svr01/fileName";
            string[] pathsplit = path.ToString().Split('/');
            string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3] + "/";
            string SecondPath = Firstpath + "/" + pathsplit[4] + "/";
            string ThirdPath = SecondPath + "/" + pathsplit[5] + "/";
            string[] paths = { Firstpath, SecondPath, ThirdPath };
            foreach (string pat in paths)
            {
               bool result= dir.EnsureDirectoryExists(pat);

                if (result==true)
                {
                    //do nothing
                }
                else
                {   //create dir
                    dir.createdir(pat);
                }
            }
            upload(path,filename);

        }
        private bool EnsureDirectoryExists(string pat)
        {

            try
            {
                //call the method the first path is exist ?
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pat);
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential("sh", "se");
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    return true;
                }
            }
            catch (Exception ex)
            { return false; }

        }
        public void createdir(string pat)
        {
            try
            {
                FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(pat));
                createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
                createdir.Credentials = new NetworkCredential("sh", "se");
                createdir.UsePassive = true;
                createdir.UseBinary = true;
                createdir.KeepAlive = false;
                FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
                Stream ftpStream1 = response1.GetResponseStream();
                ftpStream1.Close();
                response1.Close();
            }
            catch (Exception ex)
            {
            }

        }

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question