Addressing resource files in C# - file

I've added my file into resources with right click -> existing item.
Now I want to copy the added file into another directory like this:
File.Copy(#"I don't know", #"C:\Users\user-1\Desktop\", true);
I don't know what I have to write in #"I don't know" part to addressing the added resource file.

If myDir is the directory of your project, the resources' file has path myDir\Properties\Resources.resx.
Now, when you execute a program from Visual Studio in Debug mode, the folder is myDir\Bin\Debug.
You have to go up 2 folders and enter the Properties folder:
var resourcesFileName = "Resources.resx";
var currentDirectory = Directory.GetCurrentDirectory();
var actualResourceFilePath = Path.Combine(
currentDirectory, "..", "..", "Properties", resourcesFileName);
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var newFilePath = Path.Combine(desktop, resourcesFileName);
File.Copy(actualResourceFilePath, newFilePath, true);
I suggest you to not use the path separator in the hard-coded way (#"dir1\dir2...").
Use Path.Combine instead.

Related

How to find all wavs in a directory? JavaFX

I'm trying to find all the wavs in every folder that I have in a directory, for example, "Music". Right now, my code shows the wavs found in whatever folder I select, but I want it to list all the wavs found in the whole directory at once, without the user going from folder to folder. How can I do that? Here's my code:
FileChooser chooseFile = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Choose a file (*.wav)", "*.wav");
chooseFile.getExtensionFilters().add(filter);
File file = chooseFile.showOpenDialog(null);
directory = file.toURI().toString();
First you should use a DirectoryChooser to select a directory. You can use the NIO package to find the files:
DirectoryChooser chooser = new DirectoryChooser();
File directory = chooser.showDialog(null);
Path[] wavs = findWavs(directory);
private static Path[] findWavs(File directory) throws IOException {
Path dir = directory.toPath();
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE, (path, attributes) -> path.getFileName().toString().endsWith(".wav"))) {
return stream.toArray(Path[]::new);
}
}
If you need Files instead you could simply map the paths to files:
return stream.map(Path::toFile).toArray(File[]::new);

AS3 safe downloaded file directly to specified folder

May be some body have FLA file which download file to folder (without dialog of save file).
Just Click to - DOWNLOAD -> file save to folder /update on swf located. (without any confirmation of filename or filder location)
var fileName:String = "upgrade.zip";
var request:URLRequest = new URLRequest();
request.url = DOWNLOAD_URL;
var params:URLVariables = new URLVariables();
fr.download(request,fileName);
This change filename in dialog only, but dialog must to be auto-agreed.
And more one. When I try to make AS3 script, for download via URLRequest, this work only in Flash editor. FLA file (when I comp[ile to SWF -> not work).

Application showing an exception"file not found "

I created an application in VS2010 with operating system WINDOWS XP.
Now, I updated the os to WIN 7 and also updated the location of the application.
So, while running the application for opening a file using open dialog box it showing some exception like"File Not Found".
It was working fine with WIN XP, but now it showing this error, if we keep that perticular file in bin folder its working fine , but if we choose a file from other drive or a folder it showing error.
enter code here
string chosen_file = "";
ofd.Title = "Open a file";
ofd.FileName = "";
ofd.Filter = "Text Files(*.txt)|*.txt|Rich Text Box(*.rtb)|*.rtb|Word Document(*.doc)|*.doc|HTML Pages(*.htm)|*.html|Cascading Style Sheet(*.css)|*.css|JAVA(*.java)|*.java|video file(*.wmv)|*.wmv|All Files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
chosen_file = ofd.FileName;
// richTextBox2.LoadFile(chosen_file, RichTextBoxStreamType.PlainText);
var fileInfo = new FileInfo(ofd.FileName);
fileInfo.Length.ToString();
byte[] buffer = new byte[fileInfo.Length];
int length = (int)fileInfo.Length;
FileStream fileStream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.Read);
fileStream.Read(buffer, 0, length);}
My guess is you referenced a hard-coded path, probably to your "Documents" folder, rather than using an environment variable. With the change from XP to 7, that directory changed. I can't recall what the directory was for Windows XP, but it's now /users/username for Windows 7. Either way, you would be better off using an environment variable.
Look through your program for Documents and Settings and see if you can find it. If you go to a command prompt and type set, it should give you a list of environment variables you could use in place of whatever you were using.

how to create files under /WEB-INF/

I am working on an application that stores files under /WEB-INF/someFolder/. But I dont find the right way to create files under this folder. I did this, but it is not working:
File newFile = new File("/WEB-INF/fileName.xml");
When I try to check the creation:
boolean isCreated = newFile.createNewFile();
I get :
java.io.IOException: No such file or directory
Please help me doing it in the right way.
Update:
I did this workaround, it is working but I dont see that it is performant solution.
ServletContext servletContext = getServletContext();
String path = servletContext.getRealPath("/WEB-INF/");
File newFile2 = new File(path+"/fileName.xml");
Any ideas?
You shall use ServletContext.getRealPath(String) and build the entire classpath manually
String webInfPath = getServletConfig().getServletContext().getRealPath("WEB-INF");
OR go step by step:
ServletConfig scfg= getServletConfig();
ServletContext scxt = scfg.getServletContext();
String webInfPath = sxct.getRealPath("WEB-INF");
And than use the webInfPath to create a File object inside WEB-INF
File newFile = new File(webInfPath + "/fileName.xml");
make sure you applaction have the permissions to write.
you can get the path ues like this:
String path=Thread.currentThread().getContextClassLoader().getResource("com/youpackage/");
Now you get the path which is your class folder path,so you can get the WEB-INF path.
ps: i remember when create file you must writer some content,otherwies it may not create.

Copy files to another folder in a Windows Forms application

I want to copy some file from one folder to another folder of my PC using a context menu in a Windows Forms application. I know how to add a context menu, and I need to know the tricks to perform copy & paste. What type of control do I need to use?
Sample code will be highly helpful.
string dirA = #"C:\";
string dirB = #"D:\";
string[] files = System.IO.Directory.GetFiles(dirA);
foreach (string s in files) {
if (System.IO.Path.GetExtension(s).equals("bak")) {
System.IO.File.Copy(s, System.IO.Path.Combine(targetPath, fileName), true);
}
}

Resources