01. december 2005 - 12:42Der er
48 kommentarer og 1 løsning
Flytte billder fra én mappe til en anden på en server
Jeg benytter nedenstående link til at overføre filer fra en mappe på min egen computer til en mappe på serveren. Findes der en måde så jeg kun kan flytte billderne mellem 2 forud deffineret mapper på serveren?
Det kunne det muligvis godt være - jeg bruger en "knap" til at execute scriptet "<asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" />" - men får en fejlmeddelse som siger at scriptet --> indeholder ikke en diffination af "Main".
Har selvfølgelig prøvet at omdøbe "Main" til flere ting uden held.
class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; string path2 = path + "temp"; try { if (!File.Exists(path)) { // This statement ensures that the file is created, // but the handle is not kept. using (FileStream fs = File.Create(path)) {} }
// Ensure that the target does not exist. if (File.Exists(path2)) File.Delete(path2);
// Move the file. File.Move(path, path2); Console.WriteLine("{0} was moved to {1}.", path, path2);
// See if the original exists now. if (File.Exists(path)) { Console.WriteLine("The original file still exists, which is unexpected."); } else { Console.WriteLine("The original file no longer exists, which is expected."); }
prøv lige at paste din codebehind fil og din aspx fil her ind.
Synes godt om
Slettet bruger
04. december 2005 - 16:15#6
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> using System using System.IO <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <script runat="server"> class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; string path2 = path + "temp";
try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path2);
// Copy the file. File.Copy(path, path2); Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path2, true); Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch { Console.WriteLine("Double copy is not allowed, which was not expected."); } } } </script> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server"> <asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" /> </form> </body> </html>
det her virker: <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <script runat="server"> public void Main(object sender, EventArgs e) { string path = @"c:\temp\MyTest.txt"; string path2 = path + "temp";
try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path2);
// Copy the file. File.Copy(path, path2); // Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path2, true); //Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch { // Console.WriteLine("Double copy is not allowed, which was not expected."); } } </script> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server" ID="Form1"> <asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" /> </form> </body> </html>
Synes godt om
Slettet bruger
05. december 2005 - 20:25#10
public void Main(object sender, EventArgs e) { string path = @"c:\temp\MyTest.txt";<----HVOR HAVNER DENNE FIL HELT PRÆCIST? string path2 = path + "temp";
Dette er hvad jeg bruger - Det er meningen at Mytest.txt skal kopieres i mappen temp og sættes ind i mappen temp1, men intet synes at ske... temp1 forbliver bare tom :-|
prøv lige med denne her: <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <script runat="server"> public void Main(object sender, EventArgs e) { string path = @"c:\temp\MyTest.txt"; string path2 = @"c:\temp1\MyTest.txt";
try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path2);
// Copy the file. File.Copy(path, path2); // Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path2, true); //Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex); // Console.WriteLine("Double copy is not allowed, which was not expected."); } } </script> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server" ID="Form1"> <asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" /> </form> </body> </html>
tjek output vindue om der kommer nogen exceptions.
Synes godt om
Slettet bruger
07. december 2005 - 07:46#18
Stadig samme resultat! filen skifter ikke adresse...kan det tænkes at scriptet og mapperne skal ligge samme sted?
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <script runat="server"> public void Main(object sender, EventArgs e) { string path = @"c:\temp\MyTest.txt"; string path2 = @"c:\temp1\MyTest.txt";
try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path2);
// Copy the file. File.Copy(path, path2); // Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path2, true); //Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch(Exception ex) { lblfejl.Text = ex.ToString(); // Console.WriteLine("Double copy is not allowed, which was not expected."); } } </script> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server" ID="Form1"> <asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" /> <asp:label id="lblfejl" runat="server"></asp:label> </form> </body> </html>
eller aspnet brugeren skal faktisk have fuld kontrol
Synes godt om
Slettet bruger
07. december 2005 - 21:16#24
Yes! Nu virker det! Så er mit næste spørgsmål. Kan man flytte flere filer af forskellige formater på én gang? Såsom: .jpg .gif og .swf
string path = @"c:\temp\MyTest.txt,MyTest1.txt,MyTest2.txt"; string path2 = @"c:\temp1\MyTest.txt,MyTest1.txt,MyTest2.txt"; Dette eksperiment en fil i begge mapper kaldet MyTest.txt,MyTest1.txt,MyTest2.txt
string path = @"c:\temp\"; string path1 = @"c:\temp1\"; foreach (string f in Directory.GetFiles(path)) { if (f.EndsWith(".jpg") || f.EndsWith(".gif") || f.EndsWith(".swf")) { File.Copy(path + f, path1 + f); } }
Synes godt om
Slettet bruger
07. december 2005 - 22:37#26
Nu ser scriptet således ud:
<script runat="server"> public void Main(object sender, EventArgs e) { string path = @"c:\temp\"; string path1 = @"c:\temp1\"; foreach (string f in Directory.GetFiles(path)) { if (f.EndsWith("header.jpg") || f.EndsWith("tekstboks.gif") || f.EndsWith("menu.swf")) { File.Copy(path + f, path1 + f); } }
try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path1);
// Copy the file. File.Copy(path, path1); // Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path1, true); //Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch(Exception ex) { lblfejl.Text = ex.ToString(); // Console.WriteLine("Double copy is not allowed, which was not expected."); } } </script> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server" ID="Form1"> <asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" /> <asp:label id="lblfejl" runat="server"></asp:label> </form> </body> </html>
Så får jeg denne fejlmeddelse: Detaljer om undtagelse: System.NotSupportedException: Den angivne stis format understøttes ikke.
brug det her. <script runat="server"> public void Main(object sender, EventArgs e) { string path = @"c:\temp\"; string path1 = @"c:\temp1\"; foreach (string f in Directory.GetFiles(path)) { if (f.EndsWith("header.jpg") || f.EndsWith("tekstboks.gif") || f.EndsWith("menu.swf")) { File.Copy(path + f, path1 + f); } }
/*try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path1);
// Copy the file. File.Copy(path, path1); // Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path1, true); //Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch(Exception ex) { lblfejl.Text = ex.ToString(); // Console.WriteLine("Double copy is not allowed, which was not expected."); }*/ } </script> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form runat="server" ID="Form1"> <asp:Button ID="Winter" OnClick="Main" Text="Vinter" runat="server" /> <asp:label id="lblfejl" runat="server"></asp:label> </form> </body> </html>
Synes godt om
Slettet bruger
08. december 2005 - 07:46#28
Får samme fejl --> System.NotSupportedException: Den angivne stis format understøttes ikke.
prøv at fjerne denne del: /*try { // Create the file and clean up handles. using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist. File.Delete(path1);
// Copy the file. File.Copy(path, path1); // Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed. File.Copy(path, path1, true); //Console.WriteLine("The second Copy operation succeeded, which was expected."); }
catch(Exception ex) { lblfejl.Text = ex.ToString(); // Console.WriteLine("Double copy is not allowed, which was not expected."); }*/
Synes godt om
Slettet bruger
08. december 2005 - 15:04#30
Stadig samme fejl --> System.NotSupportedException: Den angivne stis format understøttes ikke :-|
NU virker den, MEN hvad skriver jeg i stien hvis jeg ønsker at benytte to mapper der ligger i samme mappe som scriptet. Jeg har prøvet mig lidt frem og tilbage men den kan ikke finde stien. Den siger:
System.IO.DirectoryNotFoundException: En del af stien "..\efteraar\" blev ikke fundet.
string path = @"..\efteraar\"; string path1 = @"..\design\"; foreach (string f in Directory.GetFiles(path))<-- highlighter denne linie som- problemets kilde...
Jeg har selvfølgelig også prøvet uden et relavtivt link.
hvor har du /efteraar og /design liggende? altså stien. og hvor ligger din side.
Synes godt om
Slettet bruger
09. december 2005 - 14:42#37
Det hele: efteraar og design samt scriptet ligger i en undermapper på min LocalHost. Det er meningen at al indhold i den mappe skal uploades på mit webhotel når projektet er færdigt.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.